How to Upload documents to Azure Blob Storage Containers Programmatically using C#

Sathish Nadarajan
 
Solution Architect
October 23, 2018
 
Rate this article
 
Views
5149

In the earlier articles, we saw how to create the Blob Storage through the Azure Console and Programmatically. Now, let us see how to upload documents to the Containers programmatically.

1. Login to the Azure Portal. Go to the Storage Account and select the Keys.

clip_image002

2. Among the two keys, make a note of any key. Both the Keys can be used for the Program. In this case, let me take the first Key’s Connection String.

3. The code is as below.

 public static void UploadFiles()
         {
             string storageConnectionString = "*****Value copied from the Console****";
 
             DirectoryInfo directoryInfo = new DirectoryInfo("D:\InputPath");
 
             var files = directoryInfo.EnumerateFiles();
 
             // Retrieve storage account from connection string.
             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
 
             // Create the blob client.
             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
 
             // Retrieve reference to a previously created container.
             CloudBlobContainer container = blobClient.GetContainerReference("<<Blob Container Name>>");
 
             foreach (FileInfo inputFile in files)
             {
                  
                 CloudBlockBlob blockBlob = container.GetBlockBlobReference("<<Folder(If required)>>\" + inputFile.Name);
 
                 blockBlob.UploadFromFile(inputFile.FullName);
             }
         }
 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment