Azure Storage - Creating Blob Container Using Storage Client Library

In this article, you will learn how to connect and create a container programmatically, using C#.NET Storage Client Library.

Please go through the articles given below to learn more about storage account.

What is Blob container?

Container is a collection, which can be used like a folder to store Blobs.

The steps are given below, which are required to create a container, using .NET Storage Client Library.

  • Download and install WindowsAzure.Storagefrom NugetPackage Library – A client library for working with Microsoft Azure storage services like Blobs, Files and Queues.
  • Build a connection string to the storage account’s end point, using Storage Account access keys.
  • Programmatically connect to the storage account and create the Storage container.

For this example, I have created a console Application, which creates a container in the selected Storage account.

Add the WindowsAzure.StorageNuget Package from NuGet.

Azure

Below are the highlighted references, which get added, as soon as I installed the package given above.

Azure
The next thing is to have the connection string for the Storage account. Grab the connection string from the access keys blade from the Storage Account, which was explained in our previous articles.

Create an AppSetting key in the App.Config to store the ConnetionString, as shown below.

  1. <?xmlversion="1.0"encoding="utf-8" ?>  
  2.     <configuration>  
  3.         <startup>  
  4.             <supportedRuntimeversion="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup>  
  5.         <appSettings>  
  6.             <addkey="ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<Storage Account name>;AccountKey=<Key>;" /> </appSettings>  
  7.     </configuration>  
Please also add System.Configuration reference to the Application, as shown below. This is required to access the App.Config App settings keys.

Azure

In order to connect to Azure Storage services, we need to include the namespaces given below.
  1. usingMicrosoft.WindowsAzure.Storage;  
  2. usingMicrosoft.WindowsAzure.Storage.Blob;  
Write the code in the Main method, as shown below. Please note that you should encapsulate the below in a separate class. The code given below is used for the demonstration.
  1. //Get the reference of the Storage Account  
  2. CloudStorageAccountstorageaccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["ConnectionString"].ToString());  
  3. //Get the reference of the Storage Blob  
  4. CloudBlobClient client = storageaccount.CreateCloudBlobClient();  
  5. //Get the reference of the Container. The GetConainerReference doesn't make a request to the Blob Storage but the Create() &CreateIfNotExists() method does. The method CreateIfNotExists() could be use whether the Container exists or not  
  6. CloudBlobContainer container = client.GetContainerReference("images");  
  7. container.CreateIfNotExists();  
Don’t execute the code yet. Let’s have a look at the containers blade, which is shown below.

Azure

As shown in the screenshot above, we don’t have any containers yet in our storage account. Let’s create the new container named ‘PDF's’ by executing the code, which we have just developed.

Execute the console Application by pressing Ctrl + F5.

Azure

The program got executed successfully. Let’s navigate to the Storage Account container’s blade and refresh the same to view the new container, which is shown below.

Azure

We have successfully created the Azure Storage Container.

Summary

We have learned the following in this article.
  • About a container.
  • How to create a reference to a Storage Account, using Storage Client Library
  • Getting a reference to a container and creating it, if it doesn’t exist yet.

Hope, you enjoyed reading the article. Your feedback is appreciated.