In order to read a blob file from a Microsoft Azure Blob Storage, you need to know the following:
- The storage account connection string. This is the long string that looks like this:
DefaultEndpointsProtocol=https;
AccountName=someaccounfname;
AccountKey=AVeryLongCrypticalStringThatContainsALotOfChars== - The blob storage container name. This is the name in the list of “Blobs”.
- The blob file name. This is the name of the blob inside the container. A file name can be in form of a path, as blobs are structured as a file structure inside the container. For ecample: folder/folder/file.extension
You also need this NuGet package:
Windows.Azure.Storage
The code is pretty simple:
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
public string GetBlob(string containerName, string fileName)
{
string connectionString = $"yourconnectionstring";
// Setup the connection to the storage account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Connect to the blob storage
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
// Connect to the blob container
CloudBlobContainer container = serviceClient.GetContainerReference($"{containerName}");
// Connect to the blob file
CloudBlockBlob blob = container.GetBlockBlobReference($"{fileName}");
// Get the blob file as text
string contents = blob.DownloadTextAsync().Result;
return contents;
}
The usage is equally easy:
GetBlob(“containername”, “my/file.json”);
Add to favorites