簡體   English   中英

使用 .NET Core 2.2 從 Azure 存儲中獲取所有 Blob

[英]Get all Blobs From Azure Storage using .NET Core 2.2

關於從 Azure 存儲(或者更准確地說是從容器)中獲取 blob 列表的非常快速的問題

由於我使用的是.NET Core 2.2 ,因此 C# 7.3 版本中不允許使用異步流:

 await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
    {
        Console.WriteLine("\t" + blobItem.Name);
    }

所以我嘗試了這樣的事情但沒有任何運氣(在黑暗中刺傷)

List<BlobItem> items = new List<BlobItem>();
Task.Factory.StartNew(async () => items.Add(await containerClient.GetBlobsAsync()));

所以我想知道 C# v7.3 中等待 foreach 語法的替代方法是什么

謝謝

GetBlobsAsync()返回的AsyncPageable<T>公開了一個IAsyncEnumerator<T> ,您可以使用它來使用簡單的while循環進行迭代:

Azure.AsyncPageable<Azure.Storage.Blobs.Models.BlobItem> blobs = containerClient.GetBlobsAsync();
IAsyncEnumerator<Azure.Storage.Blobs.Models.BlobItem> enumerator = blobs.GetAsyncEnumerator();
try
{
    while (await enumerator.MoveNextAsync())
    {
        Azure.Storage.Blobs.Models.BlobItem blob = enumerator.Current;
        // use blob
    }
}
finally
{
    await enumerator.DisposeAsync();
}

有比 while 循環選項更緊湊的語法可用於執行此操作,

AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync();

await foreach (SecretProperties secretProperties in allSecretProperties)
{
    Console.WriteLine(secretProperties.Name);
}

示例取自azure sdk 碼頭

暫無
暫無

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

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