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


Sathish Nadarajan
SharePoint MVP
Published On :   23 Oct 2018
Visit Count
Today :  3    Total :   337



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.

Categories