【问题标题】:List All Blobs from Azure Storage from a Storage URI?从存储 URI 列出 Azure 存储中的所有 Blob?
【发布时间】:2021-08-31 17:02:56
【问题描述】:

目前,我正在研究一种结构如下的存储:

AUTHORS
   > A
      > Adams
      > Agustin
      > Anderson
   > B
      > Boron
      > Brandy
   > C
      > Carson
      > Cruz

...and so on...

   > Z
      > Zeta
      > Zimbabwe
         > Zimbabwe Child Object 1
         > Zimbabwe Child Object 2

我能够使用以下代码编写遍历所有 blob 的代码:

var blobClient = new BlobServiceClient("connection-string-to-storage");
var container = blobClient.GetBlobContainerClient("Authors");
var blobsToGet = container.GetBlobs();

但是,这段代码太贵了,因为如果我要寻找说“津巴布韦”,我必须遍历所有记录,直到它的子项,不管它有多深。

我尝试了以下方法,但显然也太贵了:

var blobsToProcess = blobsToGet.Where(x => x.Name.StartsWith("Zimbabwe"));

或者,我只是使用了这些:

var cloudContainer = new CloudBlobContainer(new Uri("absolute-storage-uri-with-sas"));
var cloudDirectory = cloudContainer.GetDirectoryReference("Authors");

我能够获得具有“StorageUri.PrimaryUri”属性的 Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory 对象的集合。

[问题] 如果只有“津巴布韦”的主 URI,我将如何获取津巴布韦子对象列表

我尝试了第一个代码,但我被困在这个:

var blobClient = new BlobServiceClient("storageuri-to-zimbabwe");
// I just wanted to get the list of blobs under this storage
var container = blobClient.GetBlobContainerClient(???);
var blobsToGet = container.GetBlobs();

【问题讨论】:

  • 2 个问题:1) 您的 blob 容器的名称是“作者”。正确的?和 2) 你的 URL 是否看起来像 https://account.blob.core.windows.net/authors/Z/Zimbabwe 并且你想获取它下面的所有子项?对吗?
  • 是的,在这两个问题上。

标签: c# azure azure-blob-storage


【解决方案1】:

您可能已经知道 Azure Blob 存储中没有文件夹。它本质上是一个 2 级层次结构 - blob 容器和 blob。您看到的文件夹是虚拟的,并且是必不可少的 blob 前缀。所以在你的情况下,blob容器是authors,blob前缀是Z/Zimbabwe

考虑到你得到一个像https://account.blob.core.windows.net/authors/Z/Zimbabwe 这样的 URL,你需要做的是解析它以获得 2 个东西:blob 容器 URL 和 blob 前缀。获得这两个信息后,您可以获取以Z/Zimbabwe 前缀开头的 blob 列表。

请看下面的代码。这利用了Azure.Storage.Blobs (version 12.8.4) 包:

using System;
using System.Collections.Generic;
using Azure.Storage.Blobs;
using System.Linq;
using System.Threading.Tasks;
using Azure.Storage;
using Azure.Storage.Blobs.Models;

namespace ConsoleApp1
{
    
    class Program
    {
        static async Task Main(string[] args)
        {
            string accountName = "accouny-name";
            string accountKey = "account-key";
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
            Uri folderUri = new Uri("https://account.blob.core.windows.net/authors/Z/Zimbabwe");
            string host = folderUri.Host;//"account.blob.core.windows.net"
            //Split local path so that we can extract container name and the folder path i.e. get "authors" and "Z/Zimbabwe";
            var path = folderUri.LocalPath.Split("/", StringSplitOptions.RemoveEmptyEntries).ToList();
            //Container name will always be the 1st element in the list.
            string containerName = path[0];//"authors"
            //Remove the first element.
            path.RemoveAt(0);
            //Remaining part is the prefix
            string prefix = $"{string.Join("/", path)}/" ;//"Z/Zimbabwe/"
            //Now get the blob container URI.
            Uri blobContainerUri = new Uri($"{folderUri.Scheme}://{host}/{containerName}");
            BlobContainerClient blobContainerClient = new BlobContainerClient(blobContainerUri, credential);
            var result = blobContainerClient.GetBlobsByHierarchyAsync(delimiter:"/", prefix: prefix);
            List<string> blobFolders = new List<string>();
            List<BlobItem> blobs = new List<BlobItem>();
            string continuationToken = null;
            do
            {
                await foreach (var blobPages in result.AsPages(continuationToken))
                {
                    continuationToken = blobPages.ContinuationToken;
                    blobFolders.AddRange(blobPages.Values.Where(b => b.IsPrefix).Select(b => b.Prefix));
                    blobs.AddRange(blobPages.Values.Where(b => b.IsBlob).Select(b => b.Blob));
                }
            } while (!string.IsNullOrWhiteSpace(continuationToken));
        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 2012-03-02
    • 2019-08-17
    • 2015-04-17
    • 1970-01-01
    • 2019-12-19
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多