DEV Community

Chandra Mohan Jha
Chandra Mohan Jha

Posted on

Get blob from azure using Azure.Storage.Blobs

We can store blobs in Azure platform in C# using Azure.Storage.Blobs. In this article, we will fetch the blobs without downloading as a file.

First thing first: install NuGet package

Install NuGet package Azure.Storage.Blobs.

Import library

using Azure.Storage.Blobs;
Enter fullscreen mode Exit fullscreen mode

Get connection string

I assume you have Azure account and thus connection string to connect to Azure Blob Storage. Store this in a variable or constant based on your need.

const string CONN_STRING = <connection_string_from_azure_portal>
const string BLOB_CONTAINER = <blob_container_name>
Enter fullscreen mode Exit fullscreen mode

Now the final thing

We will fetch the blob and read it.

string filePath = "<optional_path/file_name.extension>";
var blobClient = new BlobClient(CONN_STRING, BLOB_CONTAINER, filePath);
using (var result = await blobClient.OpenReadAsync())
{
  byte[] content = new byte[result.Length];
  result.Read(content, 0, (int)result.Length);
  var jsonContent = System.Text.Encoding.Default.GetString(content);
  // Do whatever you want to do with this content
}
Enter fullscreen mode Exit fullscreen mode

That's it. I hope it would be helpful to you. 🙂

Top comments (0)