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:
CodePudding user response:
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. BecauseAsyncPageable<T>
implementsIAsyncEnumerable<T>
, you can useSystem.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)
{
...
}