Azure Blob Storage

In this article, I will explain how to create an Azure Blob Storage and upload, download files from Azure Blob Storage using C#.

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

Add a new Storage Account by clicking the Add button.
 
 

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.

  1. using Microsoft.Azure;
  2. using Microsoft.WindowsAzure.Storage;
  3. 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.

  1. string storageConnection = CloudConfigurationManager.GetSetting("BlobStorageConnectionString");  
  2. CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);  
  3. //create a block blob CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();  
  4. //create a container CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("appcontainer");  
  5. //create a container if it is not already exists  
  6. if (await cloudBlobContainer.CreateIfNotExistsAsync()) {  
  7.     await cloudBlobContainer.SetPermissionsAsync(new BlobContainerPermissions {  
  8.         PublicAccess = BlobContainerPublicAccessType.Blob  
  9.     });  
  10. }  
  11. string imageName = "Test-" + Path.GetExtension(imageToUpload.FileName);  
  12. //get Blob reference  
  13. CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(imageName);  
  14. cloudBlockBlob.Properties.ContentType = imageToUpload.ContentType;  
  15. await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream);  

Step 3.1

Download file from Azure Blob using C#.

  1. var containerName = "testcontainerherbi";  
  2.   
  3. string storageConnection = CloudConfigurationManager.GetSetting("BlobStorageConnectionString"); CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection); CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();  
  4.   
  5. CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(containerName); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference("uploadedfilename.ext");  
  6.   
  7. MemoryStream memStream = new MemoryStream();  
  8.   
  9. blockBlob.DownloadToStream(memStream);  
  10.   
  11. HttpContext.Current.Response.ContentType = blockBlob.Properties.ContentType.ToString(); HttpContext.Current.Response.AddHeader("Content-Disposition""Attachment; filename=" + blockBlob.ToString());  
  12.   
  13. HttpContext.Current.Response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString()); HttpContext.Current.Response.BinaryWrite(memStream.ToArray()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close();  
Please download the attached file.

I hope it's helpful. Eat-> Code->Sleep->Repeat.