Azure Storage - Creating A Container Using Visual Studio

This writing is a continuation of my previous one in which I have written in detail about Azure Storage, please spend some time on the previous article of mine.

Here by this writing we will be working on creating a container on Azure storage using Visual Studio.

Note

This writing is a continuation of my previous one in which I have written in detail about Azure Storage, please hspend some time on the previous article of mine.

Create an azure storage account on your azure portal. Login to the azure account and create a new storage account.

Step -1

Click on New - Storage - Storage account – blob, file, table, queue.

Azure

Configure the storage account with below options,

  • Name – name of the storage account, it should be unique.
  • Deployment model – Select Resource Manager for deployment method.
  • Account kind – Select Storage (general purpose v1) for general purpose storage account.
  • Performance – Select Standard Storage account.
  • Replication – Select Read-access geo-redundant storage (RA-GRS).
  • Secure transfer required – Disabled.
  • Subscription – Select the subscription which you own.
  • Resource Group – Create a new resource group on which the resource can be tagged.
  • Location – Select the data center location on which the resource can be deployed.

Select if you need to pin for the dashboard and click on “Create” to create the resource.

Azure

Azure

Azure

Storage account has been deployed here.
Azure

Run Visual Studio on your machine and try creating a console app on .NET Framework. Go for File - New - Project.

Azure

Select Console App (.NET Framework) under Visual C# and name your project.

Azure

And now the solution for the project will get landed up as below,

Azure

Step – 2 Add Client Library for Azure Storage Serivces – NuGet

Go for Tools - Select NuGet Packet Manager - Manage NuGet Package Manager for Solution - Add WindowsAzure.Storage package from Microsoft and check the project box and install the same.

Azure

Azure

Azure

Click on “I accept” to add the NuGet package

Azure

Step – 3 Add using namespaces

Add the following using directives to the top of the program.cs file,

  • Using Microsoft.Azure; - this is a namespace for Microsoft Windows Azure Configuration Manager Library for .NET
  • Using Microsoft.WindowsAzure.Storage; - this is a namespace for CloudStorage Account
  • Using Microsoft.WindowsAzure.Storage.Blob; - this is a namespace for Blob Storage types

    Azure

 

Step – 4 Get the Access Key to Establish a Connection – Portal

Select your storage account and get the Access Key by Copying the Connection String.

Azure

Add an AppSetting to define a ConnectionString, Go for App.config and add an appsetting to define a StorageConnectionString followed by that mention the ConnectionString which was copied before.

  1. <appSettings>  
  2.     <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=najuma;AccountKey=copZIvHIRwfJOkrg4XCYQwCxqqgPZ65cA/voAevELFFJOnAVf1dMzwVESj4lxlDWHOUYHkOey5d45npPZbhAJw==" />   
  3. </appSettings> 

 

In the above code we have defined an appSetting to create a StorageConnectionString key in order to connect to the Storage account in which the Blob need to be created.

Set the values as shown below,

  • DefaultEndpointsProtocol = https
  • AccountName = your storage account name
  • AccountKey = your account access key

    Azure

 

Step – 5 Add the ConnectionString to the program

Open program.cs and add the below code on the class which is provided by Microsoft Azure Configuration Manager Library for .NET with a configuration file.

  1. string storageconnection = System.Configuration.ConfigurationManager.AppSettings.Get("StorageConnectionString");  
  2. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageconnection);  


The System.Configuration namespaces contain types for handling configuration data, such as data in machine or application configuration files. ConfigruationManager lets your solution access Microsoft Azure settings, CloudStorageAccount class represents a Microsoft Azure Storage account using which a connection string is retrieved.

Azure

Step – 6 Add System.Configuration reference

Next, we need to add a reference to use System.Configuration Namespace in the program.  Right click on the References - Add Reference - Add System.Configuration and click on OK.

Azure

Azure

Step – 6 Create the blob container

Add the below code to create the blob container:

  1. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  2. CloudBlobContainer container = blobClient.GetContainerReference("najucontainer");  
  3. container.CreateIfNotExists();  


CloudBlobClient class will enable us to retrieve the Blob connect and access the Storage Account. After adding the above code click on save to save the code.

Azure

Azure

Build the solution and run it now on the Visual Studio.

Azure

Azure

Azure

Open your Azure portal now and check the storage account under blobs you can find the container which has been created.

Azure

Summary

Hope this article helped you out on working on the creation of containers from Visual Studio on Azure Storage. Follow my next article to upload a blob.