我正在尝试迭代容器中的 blob,但出现错误。我正在使用 Microsoft 文档中的以下代码: 我遇到的主要错误之一是调用 ToListAsync() 方法时
// Iterate through the blobs in a container
List<BlobItem> segment = await blobContainer.GetBlobsAsync(prefix: "").ToListAsync();
foreach (BlobItem blobItem in segment)
{
BlobClient blob = blobContainer.GetBlobClient(blobItem.Name);
// Check the source file's metadata
Response<BlobProperties> propertiesResponse = await blob.GetPropertiesAsync();
BlobProperties properties = propertiesResponse.Value;
// Check the last modified date and time
// Add the blob to the list if has been modified since the specified date and time
if (DateTimeOffset.Compare(properties.LastModified.ToUniversalTime(), transferBlobsModifiedSince.ToUniversalTime()) > 0)
{
blobList.Add(blob);
}
}
以下是错误:
最佳答案
来自https://learn.microsoft.com/en-us/dotnet/azure/sdk/pagination#use-systemlinqasync-with-asyncpageable
The System.Linq.Async package provides a set of LINQ methods that operate on
IAsyncEnumerable<T>
type. BecauseAsyncPageable<T>
implementsIAsyncEnumerable<T>
, you can useSystem.Linq.Async
to query and transform the data.
因此请确保您的项目中包含 System.Linq.Async 包以及 using System.Linq.Async;
C# 文件中的指令。
请注意,使用 IAsyncEnumerable<>
是有原因的。 :如果您有很多 blob,那么最好流式传输整个集合,而不是将所有值加载到内存列表中。
IAsyncEnumerable<BlobItem> segment = blobContainer.GetBlobsAsync(prefix: "");
await foreach (BlobItem blobItem in segment)
{
...
}
关于c# - Azure 迭代容器中的 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72721885/