Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Upload Image From Asp.net Core IFormFile to Azure Blob Storage?

I am trying to upload an image to azure blob storage. However when I look at the end result on azure and it just creates an empty file.

   [HttpPost("Import")]
        public IActionResult Import(IFormFile filepond)
        {
            const string accountName = "accountName";
            const string key = "key14881851";

            var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, key), true);
            var blobClient = storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference("mycontainer");
            container.CreateIfNotExistsAsync();
            container.SetPermissionsAsync(new BlobContainerPermissions()
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });

            var blob = container.GetAppendBlobReference("test.jpg");
            blob.UploadFromStreamAsync(filepond.OpenReadStream());

            return Ok();
        }

Some questions (other than why I am getting an empty file).

  1. I see alot of these methods are Async, do I need to await them for everything to work properly(ie if the container does not exist that it gets created before a file is written to the container)
  2. Do I need to wrap the UploadFromStreamAsync in a using statement.
like image 920
chobo2 Avatar asked Sep 28 '18 19:09

chobo2


People also ask

How do you upload an image to Azure blob storage?

From the left menu, select Storage accounts, then select the name of your storage account. Select Containers, then select the thumbnails container. Select Upload to open the Upload blob pane. Choose a file with the file picker and select Upload.

Can we store images in Azure blob storage?

Azure Blob Storage is Microsoft's massively scalable object storage solution for the cloud. Blob Storage is designed for storing images and documents, streaming media files, managing backup and archive data, and much more.


1 Answers

...do I need to await them for everything to work properly(ie if the container does not exist that it gets created before a file is written to the container)

Yes. Async methods return a task and you have to wait until this task is completed. That's why your file is emty.

Do I need to wrap the UploadFromStreamAsync in a using statement.

I would call it cleaner, while I'n not sure if it absolutely necessary here.

I would write it like so (not tested):

        var blobClient = storageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("mycontainer");
        await container.CreateIfNotExistsAsync();
        container.SetPermissionsAsync(new BlobContainerPermissions()
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        });

        var blob = container.GetBlockBlobReference("test.jpg");
        using(var stream = filepond.OpenReadStream()) {
            await blob.UploadFromStreamAsync(stream);
        }

Please note, that I replaced GetAppendBlobReference() with GetBlockBlobReference().

Docs with examples https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet?tabs=windows

like image 119
Christoph Lütjen Avatar answered Sep 22 '22 00:09

Christoph Lütjen