关于从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语法的替代方法是什么
谢谢
在AsyncPageable<T>
通过返回GetBlobsAsync()
暴露了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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句