亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 ListBlobsSegmentedAsync 從 Azure BLOB

如何使用 ListBlobsSegmentedAsync 從 Azure BLOB

C#
翻閱古今 2021-11-28 19:41:57
在嘗試訪問 Azure blob 文件夾的所有文件時,獲取示例代碼container.ListBlobs();但它看起來像舊的。舊代碼: container.ListBlobs();新代碼嘗試: container.ListBlobsSegmentedAsync(continuationToken);我正在嘗試使用以下代碼:container.ListBlobsSegmentedAsync(continuationToken);文件夾是這樣的:Container/F1/file.jsonContainer/F1/F2/file.jsonContainer/F2/file.json尋找更新版本以從 Azure 文件夾中獲取所有文件。任何示例代碼都會有所幫助,謝謝!
查看完整描述

3 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

這是答案的代碼:


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;


查看完整回答
反對 回復 2021-11-28
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

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


    }


查看完整回答
反對 回復 2021-11-28
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

該方法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 名稱:

http://img1.sycdn.imooc.com//61a36b32000161bb07980128.jpg

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

http://img1.sycdn.imooc.com//61a36b3d0001f4ea08510150.jpg


查看完整回答
反對 回復 2021-11-28
  • 3 回答
  • 0 關注
  • 314 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號