c# - Azure 迭代容器中的 Blob

标签 c# azure visual-studio blob blobstorage

我正在尝试迭代容器中的 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);
    }
}

以下是错误: enter image description here

enter image description here

最佳答案

来自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. Because AsyncPageable<T> implements IAsyncEnumerable<T>, you can use System.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/

相关文章:

c# - 将此 XML 文档转换为我的对象的最简单方法是什么?

c# - ASP.net CORE 相对于 Asp.net 的主要优势

c# - Winforms ReportViewer 并为 ServerReport 正确设置参数

c# - Windows Azure 服务总线 - 一般问题

Azure 持续部署失败,部署命令上出现 nuget 客户端版本错误

python-3.x - 在 azure IoT 中心上使用本地 python 创 build 备标识时出现 HTTPAPI_ERROR

c# - Restsharp 引用中的版本问题

visual-studio - 是否可以从另一个共享项目中引用共享项目?

c# - 从缓存获取对象时,代码试探性无法访问

c# - 从 C# Windows 窗体应用程序使用 Windows native 复制和移动进度条