简体   繁体   English

如何从 C# 核心中的 azure blob 存储读取所有文件

[英]How read all files from azure blob storage in C# Core

I want to read files from an azure blob storage (the files inside the folder), the blob storage contains many folders.我想从 azure blob 存储(文件夹内的文件)读取文件,blob 存储包含许多文件夹。 I want to read my folder 'blobstorage',it contains many JSON files performing.read to each file and some manipulations.我想阅读我的文件夹“blobstorage”,它包含许多 JSON 文件执行。读取每个文件和一些操作。 I tried many code that did not work:我尝试了许多不起作用的代码:

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

The above code uses 'Microsoft.WindowsAzure.Storage' nuget package.上面的代码使用'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注意:如果有任何 nuget 提到也 bcs 他们是很多包

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.您只需在下载后将其作为普通 stream 阅读即可。

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

I don't see any option to list all blob using Microsoft.WindowsAzure.Storage package.我没有看到任何使用Microsoft.WindowsAzure.Storage package 列出所有 blob 的选项。 If you can use Azure.Storage.Blobs package then try below code.如果你可以使用Azure.Storage.Blobs package 然后试试下面的代码。

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();
        }
    }
}

Output Output

在此处输入图像描述

You can also download the content of blob, Check this link您也可以下载 blob 的内容,查看此链接

You can find example code in the SDK github repo here for c#: https://github.com/Azure/azure-sdk-for-net/tree/Azure.Storage.Blobs_12.8.0/sdk/storage/Azure.Storage.Blobs/ You can find example code in the SDK github repo here for c#: https://github.com/Azure/azure-sdk-for-net/tree/Azure.Storage.Blobs_12.8.0/sdk/storage/Azure.Storage.斑点/

You can use the following command to add the package to your dotNet Core project.您可以使用以下命令将 package 添加到您的 dotNet Core 项目中。

dotnet add package Azure.Storage.Blobs

Based on the examples there, you can enumerate the blobs and then read the one you're looking for.根据那里的示例,您可以枚举 blob,然后阅读您要查找的 blob。

If you have mass data to download and are looking for efficiency, you probably don't want to download them 1 by 1 on a single thread.如果您有大量数据要下载并且正在寻找效率,您可能不想在单个线程上逐个下载它们。 Use multiple threads and async.使用多线程和异步。

Here's a good read on the topic:这是有关该主题的好读物:

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

downloading 1000 files:下载 1000 个文件:

  • with single-thread: 30seconds download time单线程:30秒下载时间
  • with multi-thread: 4seconds download time多线程:4秒下载时间

Try Below Code Out:试试下面的代码:

var connectionString = "your connection string"; 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从C#中读取Azure blob存储中的文件 - Read files in Azure blob storage from C# 如何使用C#从Azure容器/ Blob中读取/下载所有文件? - How to read/Download all files from a Azure container/Blob using C#? 使用C#将文件从FTP直接传输到Azure Blob存储 - Transferring files from FTP directly to Azure Blob storage in C# 如何使用C#从Azure存储下载所有文件? - How to download all files from Azure Storage using C#? 如何使用 c# 从 Azure Blob 存储的单个请求中下载多个文件? - How to download multiple files in a single request from Azure Blob Storage using c#? 如何删除Azure BLOB存储中的所有文件 - How to remove all files in Azure BLOB storage C#Azure将文件上载到“文件存储服务” - 而不是Blob存储 - C# Azure Upload Files to “File Storage Service” - not Blob Storage C#没有从Azure blob存储中检索blob - C# not retrieving blob from Azure blob storage 如何使用 C# 中的 Azure.Storage.Blobs 以 ByteArray 格式从 Azure 存储 blob 中获取文件 - How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C# 如何使用Azure WebJob将文件上载到Azure Blob存储? (使用C#,.NET) - How to upload files to Azure Blob Storage using Azure WebJob? (Using C#, .NET)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM