Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading text file value from Azure Blob storage

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?

like image 959
crystyxn Avatar asked Oct 23 '25 09:10

crystyxn


1 Answers

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);
    }
}

enter image description here

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.

like image 76
unknown Avatar answered Oct 24 '25 22:10

unknown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!