我想从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中找到了解决方案并且非常适合我。下载后您只需将其作为普通流读取即可。
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);
}
}
}
关于c# - 如何从 C# Core 中的 azure blob 存储读取所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61007127/