How to Create a Blob Container in Azure Storage Account Programmatically using C#

Sathish Nadarajan
 
Solution Architect
July 27, 2017
 
Rate this article
 
Views
2809

 

On the Continuation of the Earlier Article, let us see how to create the Container using C# Programmatically.

1. As usual, let us create a Console application

2. Add the NuGet Packages

a. WindowsAzure.Storage

b. WindowsAzure.ConfigurationManager

clip_image002

clip_image004

3. Make a note of the Access keys.

clip_image006

4. We can use either the key1 or key2. It does not matter.

5. The below code snippet will create the Blob Container.

 using Microsoft.SharePoint.Client;
 using Microsoft.SharePoint.Client.RecordsRepository;
 using Microsoft.WindowsAzure.Storage;
 using Microsoft.WindowsAzure.Storage.Blob;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             CreateContainer();
         }
         
         public static void CreateContainer()
         {
 
             string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=*************;AccountKey=**********;EndpointSuffix=core.windows.net";
             // Parse the connection string and return a reference to the storage account.
             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
 
             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
 
             //blobClient.
             var containers = blobClient.ListContainers();
 
             // Retrieve a reference to a container.
             CloudBlobContainer container = blobClient.GetContainerReference("<<MyContainerName>>");
 
             container.CreateIfNotExists();
 
             container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container});
         }
 
          
     }
 }
 

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