Azure blob storage is Microsoft’s offering to store unstructured object data. This data can be files, images, videos or documents etc.

Azure blob storage can serve these images and documents directly through the browser which makes Azure Blob storage a perfect choice for storing and serving website images or documents directly from blob storage.

In order to list or download blobs from azure blob storage you have to follow the below steps.

Azure.Storage.Blobs NuGet Package

Add Azure.Storage.Blobs NuGet package in your project

Azure blob storage c#

Now you need the following key credentials in order to access blob storage

  • ClientId
  • ClientSecret
  • TenantId
  • ContainerName
  • AccountName

Now the first step is to create ClientSecretCredential. This object will authenticate the user against Azure AD using Client Id and Client Secret.

var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);

Once connected with Azure AD, The next step is to initialize BlobServiceClient.

var blobserviceclient = new BlobServiceClient(new Uri(url), credential);

BlobServiceClient helps us to connect with blob containers such as

BlobContainerClient containerClient = blobserviceclient.GetBlobContainerClient(containerName);

Once you have access to the Blob container you can get blobs from the container like below

var blobs = containerClient.GetBlobs().AsPages(null, null);

Looping through the blobs

foreach (Azure.Page<BlobItem> blob in blobs)
 {
   foreach (BlobItem blobItem in blob.Values)
    {
      var b1 = containerClient.GetBlobClient(blobItem.Name);
      using (MemoryStream memoryStream = new MemoryStream())
       {
         b1.DownloadTo(memoryStream);
         string data = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
        }
   }
  }

The complete code will be as follows

public static void ProcessBlob()
 {
   var clientId = "client-id";
   var clientSecret = "client-secret";
   var tenantId = "tenant-id";
   var containerName = "container-name";
   var accountName = "account-name";
   var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
   var url = string.Format("https://{0}.blob.core.windows.net/", accountName);
   var blobserviceclient = new BlobServiceClient(new Uri(url), credential);
  try
  {
    BlobContainerClient containerClient = blobserviceclient.GetBlobContainerClient(containerName);
    var blobs = containerClient.GetBlobs().AsPages(null, null);
    
    foreach (Azure.Page<BlobItem> blob in blobs)
     {
        foreach (BlobItem blobItem in blob.Values)
          {
            var b1 = containerClient.GetBlobClient(blobItem.Name);
            using (MemoryStream memoryStream = new MemoryStream())
            {
             b1.DownloadTo(memoryStream);
             string data = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
             }
       }
      }
    }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
        }

If you want to upload images or files in Azure Blog Storage you can look at the following blog post

Categorized in: