【问题标题】:How do I get a List of Containers Name from Blob with Azure Storage Blobs? [closed]如何使用 Azure 存储 Blob 从 Blob 获取容器名称列表? [关闭]
【发布时间】:2021-02-03 17:59:32
【问题描述】:

我找到了一些使用 WindowsAzure.Storage.Blob 的答案,但是这个包已经过时了。

所以我需要使用 .net 核心从 Azure 获取 Blob 存储中的容器列表。

【问题讨论】:

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


    【解决方案1】:

    添加“Azure.Storage.Blobs”NuGet 包。

    使用 BlobServiceClient 类

    string connectionString = "";
    BlobServiceClient client = new BlobServiceClient(connectionString);
    

    使用 GetBlobContainers 或 GetBlobContainersAsync 检索容器:

    //-------------------------------------------------
    // List containers
    //-------------------------------------------------
    async static Task ListContainers(BlobServiceClient blobServiceClient, 
                                    string prefix, 
                                    int? segmentSize)
    {
        string continuationToken = string.Empty;
    
        try
        {
            do
            {
                // Call the listing operation and enumerate the result segment.
                // When the continuation token is empty, the last segment has been returned
                // and execution can exit the loop.
                var resultSegment = 
                    blobServiceClient.GetBlobContainersAsync(BlobContainerTraits.Metadata, prefix, default)
                    .AsPages(continuationToken, segmentSize);
                await foreach (Azure.Page<BlobContainerItem> containerPage in resultSegment)
                {
                    foreach (BlobContainerItem containerItem in containerPage.Values)
                    {
                        Console.WriteLine("Container name: {0}", containerItem.Name);
                    }
    
                    // Get the continuation token and loop until it is empty.
                    continuationToken = containerPage.ContinuationToken;
    
                    Console.WriteLine();
                }
    
            } while (continuationToken != string.Empty);
        }
        catch (RequestFailedException e)
        {
            Console.WriteLine(e.Message);
            Console.ReadLine();
            throw;
        }
    }
    

    prefix 参数是可选的,以防您要搜索以某个前缀开头的容器。

    代码来自documentation

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 2020-06-14
      • 2020-09-05
      • 1970-01-01
      • 2017-07-29
      • 2019-12-19
      • 2016-01-27
      • 2020-04-09
      相关资源
      最近更新 更多