Programmatically Accessing and Operating On Azure Blob Containers

Introduction: All my articles are compiled into simple steps with detailed screen shots and elaborative description. By following these steps even, a novice programmer should be able to replicate scenario. Please go through the steps and provide your valuable feedback to make my next articles more clearer and intent.

Note: I took help of various internet sources while researching this item. Credit goes to internet and Microsoft communities. This is one of the topics in Azure certification.

This is my 9th article on Azure.

Please follow below links for my previous Azure articles

Azure WebApps

Azure Logic App

Azure Event Grids

Azure SQL

Azure AD MSAL

Azure AD and User Management

Azure Key Vault With Logic App

Azure Key Vault With Data Factory

Follow below mentioned 20 steps to Access Azure Blob Containers and process programmatically by using dot net code

 After completing this blog, you will be able to do

  • Create Storage Account and Blob Containers
  • Create Program to access the storage account
  • Dot net logic to access blob containers
  • Create Blob container by using dot net program

Sample Description: We are going to create a dot net core application to access blog container. Able to create a blob container by using dot net application.

Step 1: Open Azure portal (https://portal.azure.com)

Step 2: Create a new Resource group in azure portal

Step 3: Create a new Storage Account.

Step 4: Create couple of Blob containers with in the above storage account. For more steps account storage account & Blob container creation, please visit my previous blog Click here

Step 5: Upload couple of images to newly created blobs

Step 6: Your storage account containers home page looks as below

Step 7: Now we need to grab couple of configurations from storage account

Go to Storage Account > Properties and grab the value from text box under Blob Service Primary End point. This value will be used to access your storage account

Step 8: Now go to access keys section and grab storage account name and value from key value from key1. These values will be used to access the storage account by using dot net code

Step 9: We are done with azure portal and now we need to work on dot net application.

Open visual studio code and open the folder where you want to create project.

Open terminal and execute command “dotnet new console –name ManageBlobs –output .”

Step 10: Run below command to install package, which will install the components which can be used to access blob container programmatically

“dotnet add package Azure.Storage.Blobs –version 12.0.0”

Step 11: First add below mentioned name spaces to Program.cs. These names spaces will provide access to the classes which can used to access Azure storage account and blob containner

using Azure.Storage;

using Azure.Storage.Blobs;

using Azure.Storage.Blobs.Models;

using System.Threading.Tasks;

Step 12: Declare 3 variables to store endpoint, storage account name and storage account key information at class level. Then assign the respective values we grab from Azure

Step 13: With in main method create object of  “StorageSharedKeyCredential” class and pass storage account name and storage account key. This object will create credential set to connect to storage account

StorageSharedKeyCredential accountCredentials = new StorageSharedKeyCredential(mystorageAccountName, mystorageAccountKey);

Step 14 :Create object of “BlobServiceClient” and pass Service Endpoint and StorageSharedKeyCredential object we created in previous step. This will create a client end point to access the storage account (kind of web api call)

BlobServiceClient serviceClient = new BlobServiceClient(new Uri(myblobServiceEndpoint), accountCredentials);

Step 15: Now create object of “AccountInfo” class by calling GetAccountInfoAsync method on serviceClient object. This will retrieve storage account information from azure and assigns to info variable. Now by using info object you can retrieve information like AccountKind,SkuName, etc

AccountInfo info = await serviceClient.GetAccountInfoAsync();

Step 16: Now your code looks as mentioned below

Step 17: Now run your application and observe the output. Your application will access azure storage account by using its access key and end point and retrieve information.

Step 18: Create a new method out side main and pass BlobServiceClinet as parameter. This method will navigate to the storage account> containers and access blob containers with in the storage account. Can access information of containers. Call this method from main

Step 19: Next I will create another method and will pass service client object and blob container name. This method will access the blob container and reads its items. Printing blob container item names.

Step 20: Now created another method which will take service client and container name as parameters. This method will access the storage account and accesses the containers. Will check whether any container exists with the name or not. If does not exists, it will create a new blob container with the name.

Step 21: Now run the application. First this application will access storage account and displays its information. Then it will through the containers and displays information about blob containers. Then it will go in to a specific blob container and reads its items. Finally, it will check and creates new container in Azure.

By Using above steps, you can access and process azure containers programmatically.

Happy Coding!!!