简体   繁体   中英

Azure Iterate through Blobs in a container

I'm trying through iterate through blobs from a container, but I'm getting errors. I'm using the following code from Microsoft Docs: One of the main errors I'm getting is when calling the ToListAsync() method

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

These are the errors: 在此处输入图像描述

在此处输入图像描述

From https://docs.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.

So make sure you have the System.Linq.Async package included in your project, along with a using System.Linq.Async; directive in your C# file.

Just be aware that there's a reason this is using an IAsyncEnumerable<> : if you have a lot of blobs it might be better to stream your way through the collection rather than loading all the values into an in-memory list.

IAsyncEnumerable<BlobItem> segment = blobContainer.GetBlobsAsync(prefix: "");
await foreach (BlobItem blobItem in segment)
{
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM