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.
Below are the highlighted references, which get added, as soon as I installed the package given above.
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.
- <?xmlversion="1.0"encoding="utf-8" ?>
- <configuration>
- <startup>
- <supportedRuntimeversion="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup>
- <appSettings>
- <addkey="ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<Storage Account name>;AccountKey=<Key>;" /> </appSettings>
- </configuration>
Please also add System.Configuration reference to the Application, as shown below. This is required to access the App.Config App settings keys.
In order to connect to Azure Storage services, we need to include the namespaces given below.
- usingMicrosoft.WindowsAzure.Storage;
- 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.
-
- CloudStorageAccountstorageaccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["ConnectionString"].ToString());
-
- CloudBlobClient client = storageaccount.CreateCloudBlobClient();
-
- CloudBlobContainer container = client.GetContainerReference("images");
- container.CreateIfNotExists();
Don’t execute the code yet. Let’s have a look at the containers blade, which is shown below.
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.
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.
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.