如何从C#Core中的Azure Blob存储中读取所有文件 [英] How read all files from azure blob storage in C# Core

查看:110
本文介绍了如何从C#Core中的Azure Blob存储中读取所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从azure blob存储中读取文件(文件夹内的文件),该blob存储中包含许多文件夹.我想读取我的文件夹"blobstorage",其中包含许多对每个文件执行.read的JSON文件以及一些操作.我尝试了许多无效的代码:

I want to read files from an azure blob storage (the files inside the folder), the blob storage contains many folders. I want to read my folder 'blobstorage' ,it contains many JSON files performing .read to each file and some manipulations. I tried many code that did not work:

 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference($"blobstorage");

上面的代码使用'Microsoft.WindowsAzure.Storage'nuget包.此代码无法正常工作.在堆栈溢出中发现的许多问题和答案中,我发现其中大多数已过时且无法正常工作.注意:如果有任何提及,也有bcs的话,它们是很多包装

The above code uses 'Microsoft.WindowsAzure.Storage' nuget package. This code is not working as expected. In many questions and answers found in stack overflow I found that most of them are outdated and does not work. Note: if any nuget mention that also bcs they are many packages

推荐答案

我在

I found the solution in this post and worked perfectly for me. You just have to read it as a normal stream after the download.

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#Core中的Azure Blob存储中读取所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆