Anto Subash.

Published on

dotnet file upload with ABP Blob store and Minio

Table of contents

Intro

In this post we will implement file upload using the ABP blob store and Minio

Create a new project

1abp new FileUpload

Create a container

Create the container in Domain project.

Install Volo.Abp.BlobStoring NuGet package to your Domain project

1[BlobContainerName("document")]
2public class DocumentContainer
3{
4}

Create the Entity and Entity Dto

Create the Entity in Domain project.

1public class Document : FullAuditedAggregateRoot<Guid>, IMultiTenant
2{
3    public long FileSize { get; set; }
4
5    public string MimeType { get; set; }
6
7    public Guid? TenantId { get; set; }
8
9    protected Document()
10    {
11    }
12
13    public Document(
14        Guid id,
15        long fileSize,
16        string mimeType,
17        Guid? tenantId
18    ) : base(id)
19    {
20        FileSize = fileSize;
21        MimeType = mimeType;
22        TenantId = tenantId;
23    }
24}

Create EntityDto in Contracts project

1public class DocumentDto : EntityDto<Guid>
2{
3    public long FileSize { get; set; }
4
5    public string FileUrl { get; set; }
6
7    public string MimeType { get; set; }
8}

Add mapping for the Entity and Dto in the ApplicationAutoMapperProfile class in the Application project

1CreateMap<Document, DocumentDto>().ReverseMap();

Add entity to the DBContext

Add DbSet

1public DbSet<Document> Documents { get; set; }

Configure Ef core

1builder.Entity<Document>(b =>
2{
3    b.ToTable(FileUploadConsts.DbTablePrefix + "Document", FileUploadConsts.DbSchema);
4    b.ConfigureByConvention();
5});

Add migration and update the database

To create ef migration

1dotnet ef migrations add "added_documents"

To update the database

1dotnet ef database update

Configure Minio

Install Volo.Abp.BlobStoring.Minio NuGet package to your Web and add [DependsOn(typeof(AbpBlobStoringMinioModule))] to the Web Module

Configuration is done in the ConfigureServices method of your module class

1Configure<AbpBlobStoringOptions>(options =>
2{
3    options.Containers.ConfigureDefault(container =>
4    {
5        container.UseMinio(minio =>
6        {
7            minio.EndPoint = "localhost:9900"; // your minio endPoint
8            minio.AccessKey = "AKIAIOSFODNN7EXAMPLE"; // your minio accessKey
9            minio.SecretKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"; // your minio secretKey
10            minio.BucketName = "test1"; // your minio bucketName
11        });
12    });
13});

Create Document AppService

The app service will have 2 methods one is to upload the files and another one is to view the files.

1public class DocumentAppService : FileUploadAppService
2{
3    private readonly IBlobContainer<DocumentContainer> _blobContainer;
4    private readonly IRepository<Document, Guid> _repository;
5    public DocumentAppService(IRepository<Document, Guid> repository, IBlobContainer<DocumentContainer> blobContainer)
6    {
7        _repository = repository;
8        _blobContainer = blobContainer;
9    }
10
11    public async Task<List<DocumentDto>> Upload([FromForm] List<IFormFile> files)
12    {
13        var output = new List<DocumentDto>();
14        foreach (var file in files)
15        {
16            using var memoryStream = new MemoryStream();
17            await file.CopyToAsync(memoryStream).ConfigureAwait(false);
18            var id = Guid.NewGuid();
19            var newFile = new Document(id, file.Length, file.ContentType, CurrentTenant.Id);
20            var created = await _repository.InsertAsync(newFile);
21            await _blobContainer.SaveAsync(id.ToString(), memoryStream.ToArray()).ConfigureAwait(false);
22            output.Add(ObjectMapper.Map<Document, DocumentDto>(newFile));
23        }
24
25        return output;
26    }
27
28    public async Task<FileResult> Get(Guid id)
29    {
30        var currentFile = _repository.FirstOrDefault(x => x.Id == id);
31        if (currentFile != null)
32        {
33            var myfile = await _blobContainer.GetAllBytesOrNullAsync(id.ToString());
34            return new FileContentResult(myfile, currentFile.MimeType);
35        }
36
37        throw new FileNotFoundException();
38    }
39}

Repo Link : https://github.com/antosubash/FileUpload