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.
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.
Storage account has been deployed here.
Run Visual Studio on your machine and try creating a console app on .NET Framework. Go for File - New - Project.
Select Console App (.NET Framework) under Visual C# and name your project.
And now the solution for the project will get landed up as below,
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.
Click on “I accept” to add the NuGet package
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
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.
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.
- <appSettings>
- <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=najuma;AccountKey=copZIvHIRwfJOkrg4XCYQwCxqqgPZ65cA/voAevELFFJOnAVf1dMzwVESj4lxlDWHOUYHkOey5d45npPZbhAJw==" />
- </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
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.
- string storageconnection = System.Configuration.ConfigurationManager.AppSettings.Get("StorageConnectionString");
- 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.
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.
Step – 6 Create the blob container
Add the below code to create the blob container:
- CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = blobClient.GetContainerReference("najucontainer");
- 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.
Build the solution and run it now on the Visual Studio.
Open your Azure portal now and check the storage account under blobs you can find the container which has been created.
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.