我正在尝试读取 azure blob storage
上的 .txt
文件的值。
到目前为止我的代码:
BlobServiceClient BlobServiceClient = new BlobServiceClient("connectionstring");
var containerClient = BlobServiceClient.GetBlobContainerClient("staging");
var blobClient2 = containerClient.GetBlockBlobClient($"myfile.txt");
var date = blobClient2.DownloadAsync().Result;
但这会返回元数据,如何获取 .txt
文件中的实际文本?
最佳答案
<强>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
具有内容属性。
<强>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()
用于旧版本。
关于c# - 从 Azure Blob 存储读取文本文件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65410114/