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


Sathish Nadarajan
SharePoint MVP
Published On :   27 Jul 2017
Visit Count
Today :  7    Total :   446
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


 

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.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

SharePoint Analytics