Please go through the following articles to learn more about Storage Account.
- Azure Storage – Basics
- Azure Resource Manage Template: Create A Storage Account Using Blank Template
- Create a Storage Account and learn how to access It Programmatically
What is Blob Container?
Container is a collection that could be used like a folder for storing the Blobs.
Below are the steps required for creating a Container using .NET Storage Client Library.
- Download and Install Storage from Nuget Package 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 that creates a Container in the selected Storage Account.
Add the WindowsAzure.Storage Nuget Package from Nuget.
Below are the highlighted references that gets added as soon as I installed the above Package.
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 a Storage Account and learn how to access It Programmatically
Create an AppSetting key in the App.Config to store the ConnetionString as shown below.
<?xml version=”1.0″ encoding=”utf-8″ ?>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.6.1″ />
<add key=”ConnectionString” value=”DefaultEndpointsProtocol=https;AccountName=<StorageAccountname>;AccountKey=<Key>;”/>
Please also add System.Configuration reference to the application as shown below. This is required for accessing the App.Config App Settings keys.
In order to connect to Azure Storage services, we need to include the following namespaces.
Write the following code in the Main method as shown below. Please note that you should encapsulate the below in a separate class. The below code is used for demonstration.
//Get the reference of the Storage Account
CloudStorageAccount storageaccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings[“ConnectionString”].ToString());
//Get the reference of the Storage Blob
CloudBlobClient client = storageaccount.CreateCloudBlobClient();
/*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*/
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 above screen capture, we don’t have any Containers yet in our storage account. Let’s create the new container named ‘pdfs’ by executing the code that 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.
That’s it. We have successfully created the Azure Storage Container.
Summary: We have learnt the following in this article.
- About a Container
- How to create a reference to a Storage Account using Storage Client Library
- Get a reference to a Container and create it if doesn’t exist yet.
Hope you enjoyed reading the article. Your feedback is appreciated.