簡體   English   中英

如何使用 ListBlobsSegmentedAsync 從 Azure BLOB 中的目錄中獲取所有文件

[英]How to get all files from a directory in Azure BLOB using ListBlobsSegmentedAsync

嘗試訪問 Azure blob 文件夾的所有文件時,獲取container.ListBlobs(); 但是它看起來像一個舊的。

舊代碼: container.ListBlobs();

新代碼嘗試: container.ListBlobsSegmentedAsync(continuationToken);

我正在嘗試使用以下代碼:

container.ListBlobsSegmentedAsync(continuationToken);

文件夾如下:

Container/F1/file.json
Container/F1/F2/file.json
Container/F2/file.json

尋找更新版本以從 Azure 文件夾中獲取所有文件。 任何示例代碼都會有所幫助,謝謝!

C#代碼:

   //connection string
    string storageAccount_connectionString = "**NOTE: CONNECTION STRING**";

    // Retrieve storage account from connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference("**NOTE:NAME OF CONTAINER**");
    //The specified container does not exist

    try
    {
        //root directory
        CloudBlobDirectory dira = container.GetDirectoryReference(string.Empty);
        //true for all sub directories else false 
        var rootDirFolders = dira.ListBlobsSegmentedAsync(true, BlobListingDetails.Metadata, null, null, null, null).Result;

        foreach (var blob in rootDirFolders.Results)
        {
             Console.WriteLine("Blob", blob);
        }

    }
    catch (Exception e)
    {
        //  Block of code to handle errors
        Console.WriteLine("Error", e);

    }

這是答案的代碼:

private async Task<List<IListBlobItem>> ListBlobsAsync(CloudBlobContainer container)
{
    BlobContinuationToken continuationToken = null;
    List<IListBlobItem> results = new List<IListBlobItem>();
    do
    {
       bool useFlatBlobListing = true;
       BlobListingDetails blobListingDetails = BlobListingDetails.None;
       int maxBlobsPerRequest = 500;
       var response = await container.ListBlobsSegmentedAsync(BOAppSettings.ConfigServiceEnvironment, useFlatBlobListing, blobListingDetails, maxBlobsPerRequest, continuationToken, null, null);
            continuationToken = response.ContinuationToken;
            results.AddRange(response.Results);
        }
     while (continuationToken != null);
     return results;
}

然后你可以返回如下值:

IEnumerable<IListBlobItem> listBlobs = await this.ListBlobsAsync(container);
foreach(CloudBlockBlob cloudBlockBlob in listBlobs)
  {
     BOBlobFilesViewModel boBlobFilesViewModel = new BOBlobFilesViewModel
     {
          CacheKey = cloudBlockBlob.Name,
          Name = cloudBlockBlob.Name
      };
      listBOBlobFilesViewModel.Add(boBlobFilesViewModel);
   }
//return listBOBlobFilesViewModel;

更新:使用Azure.Storage.Blobs v12從目錄中獲取所有文件名 - 包

var storageConnectionString = "DefaultEndpointsProtocol=...........=core.windows.net";
var blobServiceClient = new BlobServiceClient(storageConnectionString);

//get container
var container = blobServiceClient.GetBlobContainerClient("container_name");

List<string> blobNames = new List<string>();

//Enumerating the blobs may make multiple requests to the service while fetching all the values
//Blobs are ordered lexicographically by name
//if you want metadata set BlobTraits - BlobTraits.Metadata
var blobHierarchyItems = container.GetBlobsByHierarchyAsync(BlobTraits.None, BlobStates.None, "/");

await foreach (var blobHierarchyItem in blobHierarchyItems)
{
    //check if the blob is a virtual directory.
    if (blobHierarchyItem.IsPrefix)
    {
        // You can also access files under nested folders in this way,
        // of course you will need to create a function accordingly (you can do a recursive function)
        // var prefix = blobHierarchyItem.Name;
        // blobHierarchyItem.Name = "folderA\"
        // var blobHierarchyItems= container.GetBlobsByHierarchyAsync(BlobTraits.None, BlobStates.None, "/", prefix);     
    }
    else
    {
        blobNames.Add(blobHierarchyItem.Blob.Name);
    }
}

您可以在此處找到更多選項和示例。

這是 nuget 包的鏈接

CloudBlobClient.ListBlobsSegmentedAsync方法用於返回包含容器中的 blob 項集合的結果段。

要列出所有 blob,我們可以使用ListBlobs方法,

這是一個演示供您參考:

    public static List<V> ListAllBlobs<T, V>(Expression<Func<T, V>> expression, string containerName,string prefix)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("YourConnectionString;");

        CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName);
        container.CreateIfNotExists();

        var list = container.ListBlobs(prefix: prefix,useFlatBlobListing: true);

        List<V> data = list.OfType<T>().Select(expression.Compile()).ToList();
        return data;
    }

用法和截圖:

列出一個文件夾下的所有 blob 名稱:

列出一個文件夾下的所有 blob 名稱

列出一個文件夾下的所有 blob 的 URL:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM