c# - 如何从 C# Core 中的 azure blob 存储中读取所有文件
问题描述
我想从 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 提到 bcs 他们是很多包
解决方案
我在这篇文章中找到了解决方案,并为我完美地工作。您只需在下载后将其作为普通流读取即可。
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);
}
}
}
推荐阅读
- flutter - Flutter 中 Futurebuilder 中的快照究竟意味着什么?
- java - PKIX 路径构建 - 通过 InvokeHTTP 处理器调用 API 时 NIFI 抛出错误
- node.js - 获取 Mongodb 数据到 Node 和 React
- intellij-idea - Lombok plugin intellij - 代码重新格式化不适用于构建器
- java - 在这个一对一的双向映射中我做错了什么?
- pine-script - NEOWave Wave Chart - 我可以在特定价格柱上启动 Pine Script 指标吗?
- android - 你好。即使使用 al 属性,也无法在 Appium 中找到文本框(元素)。Appium 中的错误
- bluetooth - 带有 efusA7UL 和 BM83 的 Copckit 能够在 QT 应用程序中管理不同的蓝牙配置文件吗?
- image-processing - 我有一个 GAN 输出,我可以将以下输出序列中的图像保存为单个图像吗?
- android - 使用 Flutter 在 Android 中添加 Home 小部件