Today, I will explain how to create Azure Blob Storage and upload and download files from Azure Blob Storage using C#.
Step 1 - Create Azure Blob Storage
Link - Portal.Azure.com.
In the left menu, click on Storage. It will take you to the Microsoft Storage account.

Step 2

Navigate to the Blob Service section, as shown below.
Next, create a container for the Blob to add files into the container using the Upload button, as shown below.

Navigate to the Storage Account (which you created) -> AccessKey, copy connection string which is used to connect to the Blob from C# code.
Step 3 - Programming part
Add the required reference using NuGet.
- using Microsoft.Azure;
- using Microsoft.WindowsAzure.Storage;
- using Microsoft.WindowsAzure.Storage.Blob;
NuGet Package Manager is,
Install-Package WindowsAzure.Storage -Version 9.3.2
Add the Azure Storage Account Connection String in App.config which I explained in the previous screenshot. Copy the below code and reference the file which is to be uploaded. The code will work like a charm.
- string storageConnection = CloudConfigurationManager.GetSetting("BlobStorageConnectionString");
- CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);
- //create a block blob CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
- //create a container CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("appcontainer");
- //create a container if it is not already exists
- if (await cloudBlobContainer.CreateIfNotExistsAsync()) {
- await cloudBlobContainer.SetPermissionsAsync(new BlobContainerPermissions {
- PublicAccess = BlobContainerPublicAccessType.Blob
- });
- }
- string imageName = "Test-" + Path.GetExtension(imageToUpload.FileName);
- //get Blob reference
- CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(imageName);
- cloudBlockBlob.Properties.ContentType = imageToUpload.ContentType;
- await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream);
Step 3.1
- var containerName = "testcontainerherbi";
- string storageConnection = CloudConfigurationManager.GetSetting("BlobStorageConnectionString"); CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection); CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
- CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(containerName); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference("uploadedfilename.ext");
- MemoryStream memStream = new MemoryStream();
- blockBlob.DownloadToStream(memStream);
- HttpContext.Current.Response.ContentType = blockBlob.Properties.ContentType.ToString(); HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" + blockBlob.ToString());
- HttpContext.Current.Response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString()); HttpContext.Current.Response.BinaryWrite(memStream.ToArray()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close();
I hope it's helpful. Eat-> Code->Sleep->Repeat.

JosephsPosted Sep 24, 2019, 5:21 AM
Await method
JosephsPosted Sep 24, 2019, 5:20 AM
How to get this CloudBlockBlob .uri Await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream);
JosephsPosted Sep 24, 2019, 5:20 AM
Await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream);
JosephsPosted Sep 24, 2019, 5:20 AM
Are you sure retrieve this line ?
JosephsPosted Sep 24, 2019, 4:42 AM
How to receive StorageUri after this line invoked await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream)
Tushant RajpalPosted Aug 2, 2019, 6:48 AM
The link from where it got pasted is :-- https://community.dynamics.com/365/b/heralddyncrm/posts/upload-and-download-files-in-azure-blob-using-c
Tushant RajpalPosted Aug 2, 2019, 6:48 AM
Please do not share the copy paste work. Please include your work also.
Sreekanth ReddyPosted Jan 17, 2019, 3:33 AM
Hi bro, continue the series on azure.. Thank you.