c# - 如何从 C# Core 中的 azure blob 存储读取所有文件

标签 c# azure .net-core azure-storage azure-blob-storage

我想从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/

相关文章:

ServiceBus 的 Azure ARM 模板 : cannot reference itself

c# - 如何使用 C# 在 .NET Core 中挂接键盘事件

azure - 我可以在 azure 中查看 Web 应用程序的控制台日志吗?

c# - Mvvm Light 和 Visual C# Express?

c# - 创建缩略图

c# - 动态创建 X 个类对象

c# - 从 Azure Blob 存储下载文件

sdk - .NET Core 中的 SDK 和 Runtime 有什么区别?

.net - 使用 .NET Core 和 xUnit 同时针对多个框架的单元测试代码

c# - 混合 Lucene/MySQL 查询或概念