【问题标题】:How read all files from azure blob storage in C# Core如何从 C# Core 中的 azure blob 存储中读取所有文件
【发布时间】:2020-07-15 08:12:36
【问题描述】:

我想从 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 他们是很多包

【问题讨论】:

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


    【解决方案1】:

    我在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);
        }
      }
    }
    

    【讨论】:

    • 工作,但是这段代码的性能很差。
    【解决方案2】:

    我没有看到任何使用 Microsoft.WindowsAzure.Storage 包列出所有 blob 的选项。如果您可以使用Azure.Storage.Blobs 包,请尝试以下代码。

    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    using System;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static string connectionString = "DefaultEndpointsProtocol=https;AccountName=storage******c9709;AccountKey=v**************************************;EndpointSuffix=core.windows.net";
            static string container = "azure-webjobs-hosts";
            static void Main(string[] args)
            {
                // Get a reference to a container named "sample-container" and then create it
                BlobContainerClient blobContainerClient = new BlobContainerClient(connectionString, container);
                blobContainerClient.CreateIfNotExists();
                Console.WriteLine("Listing blobs...");
                // List all blobs in the container
                var blobs = blobContainerClient.GetBlobs();
                foreach (BlobItem blobItem in blobs)
                {
                    Console.WriteLine("\t" + blobItem.Name);
                }            
                Console.Read();
            }
        }
    }
    

    输出

    你也可以下载blob的内容,看这个link

    【讨论】:

    • 它只是列出所有文件。我想读取特定文件夹中的所有文件
    • 你也可以下载blob检查更新答案的内容,我加了链接
    • 我不全是blob,只指定文件夹文件和读取文件内容,无法下载
    • 容器内的文件夹只是虚拟文件夹,您可以从特定容器中读取所有文件和文件夹,然后您可以过滤并仅下载所需的文件夹 JSON 文件。
    【解决方案3】:

    您可以在 SDK github repo 中找到 c# 的示例代码: https://github.com/Azure/azure-sdk-for-net/tree/Azure.Storage.Blobs_12.8.0/sdk/storage/Azure.Storage.Blobs/

    您可以使用以下命令将包添加到您的 dotNet Core 项目中。

    dotnet add package Azure.Storage.Blobs
    

    根据那里的示例,您可以枚举 blob,然后读取您要查找的那个。

    【讨论】:

      【解决方案4】:

      如果您有大量数据要下载并且正在寻找效率,您可能不想在单个线程上逐个下载它们。使用多线程和异步。

      这是一个很好的主题阅读:

      https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-scalable-app-download-files?tabs=dotnet

      下载 1000 个文件:

      • 单线程:30秒下载时间
      • 多线程:4秒下载时间

      【讨论】:

        【解决方案5】:

        试试下面的代码:

        var connectionString = "你的连接字符串";

                CloudStorageAccount storageacc = CloudStorageAccount.Parse(connectionString);
                //Create Reference to Azure Blob
                CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference("containerName");
                var blobs = container.GetDirectoryReference("FolderName").GetDirectoryReference("FolderName").ListBlobs().OfType<CloudBlockBlob>().ToList();
                Console.WriteLine("Total files found in directory: " + blobs.Count.ToString());
                var tempBlobList = blobs.Where(b => b.Name.Contains("fileName")).ToList();
                var response = await tempBlobList[0].DownloadTextAsync();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-16
          • 2021-10-18
          • 1970-01-01
          • 2020-12-18
          • 2021-04-01
          • 1970-01-01
          • 2021-05-09
          相关资源
          最近更新 更多