dinotom
dinotom

Reputation: 5162

Iterating through blobs in Azure SDK v12

I am upgrading a web app to use Azure SDK v 12.10

Before this, I was grabbing all the blobs in a virtual path from the container into a list and iterating through them.

var results = await blobContainer.
                ListBlobsSegmentedAsync("publicfiles/images/" + customer.AzureFolderName, true, BlobListingDetails.All, 100, blobContinuationToken, null, null);
            // Get the value of the continuation token returned by the listing call.
            blobContinuationToken = results.ContinuationToken;

            foreach (var item in results.Results)
            {
                var filename = GetFileNameFromBlobUri(item.Uri, customer.AzureFolderName);
                var img = new EvaluationImage
                {
                    ImageUrl = item.Uri.ToString(),
                    ImageCaption = GetCaptionFromFilename(filename),
                    IsPosterImage = filename.Contains("poster"),
                    ImagesForCompany = customer.CompanyName
                };

                images.Add(img);
            }

All I can find from googling how to do so in the new SDK, yields this

await foreach (BlobItem blob in blobContainer.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"publicfiles/images/{customer.AzureFolderName}"))

The problem is the "blob" variable (BlobItem) has minimal useable properties, for example, I need the Uri which was available before. The "blob" variable in prior versions had a multitude of useable properties. Is there some other type (not BlobItem) that has these properties that I'm not finding in my searching?

I don't see anything useable in BlobTraits or BlobStates either that change this.

Upvotes: 0

Views: 982

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136366

It's not as straight forward to get the URI of the blob in SDK version 12.x. When you get a BlobItem as part of listing result, you will need to create a BlobClient object out of it and then you will be able to get the URI of the blob.

Here's the sample code to do so:

BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(container);
var listingResult = containerClient.GetBlobsAsync();
await foreach (var blobItem in listingResult)
{
    BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);
    Uri blobUri = blobClient.Uri;
    //Do something with the URI...
}

Code above makes use of Azure.Storage.Blobs (12.9.1) SDK.

Upvotes: 1

Related Questions