I am trying to read the value of a .txt file I have on azure blob storage.
My code so far:
BlobServiceClient BlobServiceClient = new BlobServiceClient("connectionstring");
var containerClient = BlobServiceClient.GetBlobContainerClient("staging");
var blobClient2 = containerClient.GetBlockBlobClient($"myfile.txt");
var date = blobClient2.DownloadAsync().Result;
But this returns metadata, how do I get the actual text within the .txt file?
1. Azure Blob storage client library v12 for .NET:
var blobClient2 = container.GetBlockBlobClient("test.txt");
BlobDownloadInfo download = blobClient2.Download();
var content = download.Content;
using (var streamReader = new StreamReader(content))
{
while (!streamReader.EndOfStream)
{
var line = await streamReader.ReadLineAsync();
Console.WriteLine(line);
}
}

BlobDownloadInfo has the content property.
2. Azure Blob storage client library v11 for .NET:
CloudStorageAccount storageAccount = CloudStorageAccount storageAccount = CloudStorageAccount.Parse("StorageConnectionString");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
var content = await blockBlob.DownloadTextAsync();
DownloadTextAsync() is used for the older version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With