如何从C#Core中的Azure Blob存储中读取所有文件

问题描述 投票:0回答:1

我想从azure blob存储中读取文件。文件夹中的文件。 Blob存储包含许多文件夹。我想读取我的文件夹“ blobstorage”,其中包含许多json文件。读取每个文件并执行一些操作。我尝试许多代码在我的情况下对任何代码都不起作用。

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

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

c# azure .net-core azure-storage azure-storage-blobs
1个回答
0
投票

我看不到使用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();
        }
    }
}

输出

enter image description here

您也可以下载blob的内容,请检查此link

© www.soinside.com 2019 - 2024. All rights reserved.