Monday, May 23, 2022

Upload files in blob Container -azure storage in .Net Core

Lets follow steps to Upload files in Blob container

I will use same project which I created earlier , kindly see https://mannojkumar.blogspot.com/2022/05/upload-files-in-azure-file-storage-in.html for more details ..

1. Login to azure portal :  https://portal.azure.com/

2. Go to Storage Account and Click on Container from left panel ->Add Container -> enter container name (lowercase) -> create

 if you dont want to create container from here leave the 2nd steps , it will be created automatically from code .


3. Open visual studio to setup project see the previous blog https://mannojkumar.blogspot.com/2022/05/upload-files-in-azure-file-storage-in.html.

4. Update appsettings.json file and add your created container name / want to create in azure blob 
{
  "AppSettings": {
    "DownloadPath": "url",
    "AzureFileConnection": "",
    "AzureAccessKey1": "",
    "AzureAccessKey2": "",
    "AzureFileShare": "azuretest",
    "AzureDirectory": "myfiles",
    "BlobContainer": "mycontainer"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  }
}
.

Add BlogContainer property in AppSettings.cs class 
 public string BlobContainer { get; set; }



5. Add below code in FileUploadService

    public int FileUploadInBlobContainer(FileDto command)
        {
            string fileName;
            byte[] fileInBytes;
            var file = command.File;
            fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
            fileInBytes = GetBytes(file);
            if (fileInBytes == null || fileInBytes.Length == 0)
            {
                return 0;
            }
            try
            {
                BlobServiceClient blobServiceClient = new BlobServiceClient(_appSetting.AzureFileConnection);

                var blobContainerClient = blobServiceClient.GetBlobContainerClient(_appSetting.BlobContainer);
                blobContainerClient.CreateIfNotExists();
                using (MemoryStream stream = new MemoryStream(fileInBytes))
                {
                    blobContainerClient.UploadBlob(fileName, stream);
                }

             
            }
            catch (Exception ex)
            {
                // Log.Error("can't use storage account! " + ex.Message);
                throw ex; // you can handle the way how u want
                // return null;
            }
            return 1;
        }

6. add below to IFileUploadService

int FileUploadInBlobContainer(FileDto command);

7. Add below endpoint in FileUploadController

 [HttpPost, DisableRequestSizeLimit]

        [Route("upload-files-blob")]
        [Consumes("multipart/form-data")]
        public IActionResult UploadFilesBlobs(FileDto command)
        {
            var result = _fileService.FileUploadInBlobContainer(command);
            return Ok(result);

      }

8. Run your project and browser swagger and click execute



9. Go to azure storage-> container ->mycontainer you can see your uploaded file 

We are done with uploading file in blob container . in next post we will see how to download files  from blob. 


Thanks

No comments:

Post a Comment

Thanks for your valuable comments

Convert Html to Pdf in azure function and save in blob container

 In this post  I am going to create an azure function ( httpTrigger ) and send html content  which will be converted into PDF and save in bl...