我想从azure blob存储中读取文件(文件夹内的文件),blob存储包含许多文件夹。
我想读取我的文件夹“blobstorage”,它包含许多 JSON 文件,对每个文件执行 .read 和一些操作。我尝试了很多代码都不起作用:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference($"blobstorage");
上面的代码使用“Microsoft.WindowsAzure.Storage”nuget 包。此代码未按预期工作。
在堆栈溢出中发现的许多问题和答案中我发现大多数都已经过时并且不起作用。
注意:如果任何 nuget 也提到它们是很多包
我在这找到了解决方案post https://www.pritambaldota.com/reading-string-content-from-azure-blob-storage-using-csharp/并且非常适合我。下载后您只需将其作为普通流读取即可。
BlobServiceClient blobServiceClient = new BlobServiceClient("connectionString");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("containerName");
BlobClient blobClient = containerClient.GetBlobClient("blobName.csv");
if (await blobClient.ExistsAsync())
{
var response = await blobClient.DownloadAsync();
using (var streamReader= new StreamReader(response.Value.Content))
{
while (!streamReader.EndOfStream)
{
var line = await streamReader.ReadLineAsync();
Console.WriteLine(line);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)