Reading string content from Azure Blob Storage using CSharp (C#)

How to read string content from Azure Blob Storage using CSharp (C#) is very common scenario. For e.g if you want to read CSV blob. Azure Blob storage .Net client library v12 is recommended package, but there is no direct API to achieve this easily.

Microsoft released Azure Blob storage client library v12 for .NET which is recommended library to work with Azure blob storage going forward. This new SDK is refactored into specific nuget packages based on resources. For e.g. if you want to work with Azure Blob Storage then use Microsoft.Azure.Storage.Blob .Net Client package. You can install this via dotnet add package Microsoft.Azure.Storage.Blob command. This package has differences in API signatures as compared to earlier legacy v11 SDK.

To read serialized string content from blob, there is no direct API available for e.g. reading CSV content line by line. Instead of serialized string, the API will return response content Memory Stream. You can use StreamReader Api to read the stream at ones or line by line easily with ReadLineAsync() or ReadToEndAsync() api from StreamReader class from System.IO namespace.

The Microsoft.Azure.Stroage.Blob SDK provides the BlobServiceClient which allows you to manipulate Azure Storage service resources and blob containers.

BlobServiceClient blobServiceClient = new BlobServiceClient("connectionString");

Once you get reference of BlobServiceClient, you can call GetBlobContainerClient() api from blob service client object to get the BlobContainerClient which allows you to manipulate Azure Storage containers and their blobs.

BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("containerName");

After you get BloblContainerClient, you can get reference of specific blob by GetBlobClient() Api which create a new BlobClient object by appending blobName to the end of Uri.

BlobClient blobClient = containerClient.GetBlobClient("blobName.csv");

You can later call DownloadAsyn() method on BlobClient to download the blob Response Stream, which you can read by StreamReader.ReadLineAsyn() api.

var response = await blobClient.DownloadAsync();
  using (var streamReader= new StreamReader(response.Value.Content))
  {
    while (!streamReader.EndOfStream)
    {
      var line = await streamReader.ReadLineAsync();
      Console.WriteLine(line);
    }
  }

Complete source code will be as below –

BlobServiceClient blobServiceClient = new BlobServiceClient("connectionString");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("containerName");
BlobClient blobClient = containerClient.GetBlobClient("blobName.csv");
if (await blobClient.ExistsAsync())
{
  var response = await blobClient.DownloadAsync();
  using (var streamReader= new StreamReader(response.Value.Content))
  {
    while (!streamReader.EndOfStream)
    {
      var line = await streamReader.ReadLineAsync();
      Console.WriteLine(line);
    }
  }
}

Hopefully, this article helped you figure out how to get this working.