Upload And Download Files From Blob Storage Using C#

This is the second part of the Introduction to Azure Blob storage. Here we will see how to access the azure blog storage for uploading and downloading files using C#.
If you do not have the storage account, please read the first part of this article to create an Azure storage account.
Once we have created the Azure storage account and container let’s develop a simple C# console application to upload and download the files to the blob storage programmatically.
First, create a C# console application
To access Azure storage account we need to install the NuGet package WindowsAzure.Storage
I have installed the latest version v9.3.3
After the package has been installed, we need to include the following references in our application.
  1. using Microsoft.Azure;    
  2. using Microsoft.WindowsAzure.Storage;    
  3. using Microsoft.WindowsAzure.Storage.Blob; 
In the main method, I have created 2 methods
1. Upload_ToBlob(local_file_Path, Azure_container_Name) - To upload the file to the Blob storage
2. download_FromBlob(filename_with_Extention, Azure_container_Name) – To download the file from the Blob storage
Please refer the code snippet below
  1. static void Main(string[] args) {  
  3.  AzureBlobSample azureBlob = new AzureBlobSample();  
  5.  azureBlob.upload_ToBlob("D:\\data_d.txt""mydatacontainer");  
  6.  azureBlob.download_FromBlob("data_d.txt""mydatacontainer");  
  8.  Console.ReadKey();  
  9. }  

Upload files to the Blob storage

The method definition for the Upload_ToBlob() is given below
Here I am using 2 parameters for the method.
One for the file name and the other for the azure container name
  1. public void upload_ToBlob(string fileToUpload, string azure_ContainerName) {  
  2.  Console.WriteLine("Inside upload method");  
  4.  string file_extension,  
  5.  filename_withExtension,  
  6.  storageAccount_connectionString;  
  7.  Stream file;  
  9.  //Copy the storage account connection string from Azure portal     
  10.  storageAccount_connectionString = "your Azure storage account connection string here";  
  12.  // << reading the file as filestream from local machine >>    
  13.  file = new FileStream(fileToUpload, FileMode.Open);  
  15.  CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);  
  16.  CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();  
  17.  CloudBlobContainer container = blobClient.GetContainerReference(azure_ContainerName);  
  19.  //checking the container exists or not  
  20.  if (container.CreateIfNotExists()) {  
  22.   container.SetPermissionsAsync(new BlobContainerPermissions {  
  23.    PublicAccess =  
  24.     BlobContainerPublicAccessType.Blob  
  25.   });  
  27.  }  
  29.  //reading file name & file extention    
  30.  file_extension = Path.GetExtension(fileToUpload);  
  31.  filename_withExtension = Path.GetFileName(fileToUpload);  
  33.  CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename_withExtension);  
  34.  cloudBlockBlob.Properties.ContentType = file_extension;  
  36.  cloudBlockBlob.UploadFromStreamAsync(file); // << Uploading the file to the blob >>  
  38.  Console.WriteLine("Upload Completed!");  
  40. }  
In the above method, we copied the storage account connection string from Azure portal.
You can find the connection string by clicking the Access Keys menu in the Settings section.
Please refer to the screenshot below. 
Copy the connection string and assign to the storageAccount_connectionString variable in the upload_ToBlob() method
  1. storageAccount_connectionString = "your Azure storage account connection string"   
The below 3 statements are used to create the objects for the Storage Account, Blob client and Blob container 
  1. CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);  
  2. CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();  
  3. CloudBlobContainer container = blobClient.GetContainerReference(azure_ContainerName);   
The below code snippet is checking whether the specified container exists in storage account or not.
If it exists, the application will use the existing container. Otherwise, it will create a container inside storage account with specified name.
  1. if (container.CreateIfNotExists())    
  2. {      
  3.     container.SetPermissionsAsync(new    
  4.     BlobContainerPermissions    
  5.     {    
  6.         PublicAccess =    
  7.                    BlobContainerPublicAccessType.Blob    
  8.     });  
  9. }  
The below statement is used to create a Block blob object using the file name with extension
  1. CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename_withExtension);   
cloudBlockBlob.UploadFromStreamAsync(file) statement is used to upload the file to the blob storage.

Download files from the Blob storage

In my implementation, I have used 2 parameters for the download_FromBlob() method.
One is the File Name and the other one is the Azure container name.
The definition for the download_FromBlob() method is given below.
  1. public void download_FromBlob(string filetoDownload, string azure_ContainerName) {  
  2.   Console.WriteLine("Inside downloadfromBlob()");  
  4.   string storageAccount_connectionString = "Paste you storage account connection string here";  
  6.   CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);  
  7.   CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();  
  9.   CloudBlobContainer container = blobClient.GetContainerReference(azure_ContainerName);  
  10.   CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filetoDownload);  
  12.   // provide the file download location below            
  13.   Stream file = File.OpenWrite(@ "E:\Tools\BlobFile\" +filetoDownload);    
  15.    cloudBlockBlob.DownloadToStream(file);  
  17.    Console.WriteLine("Download completed!");  
  19.   }  
Obviously the codes are the same as the upload method.
cloudBlockBlob.DownloadToStream(file) statement is used to download the file from the blob storage.
Since it is a basic application, I haven’t used any validation to check whether the file and the container exists or not.
You can add more logic to make the application more secure and accurate. 
Happy Coding