Coolguy
Coolguy

Reputation: 2285

List down uri of all blobs in azure container

I know that we can use the below to get the list of blobs in a Azure storage container:

var list = fileContainer.ListBlobs(useFlatBlobListing: true);
List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();

Then how bout getting the list of uri of all the blobs?

I tried the below but failed cos got error:

List<string> blobNamesUri = list.OfType<CloudBlockBlob>().Select(b => b.Name.Uri.ToString()).ToList();

Upvotes: 0

Views: 711

Answers (2)

Lee Liu
Lee Liu

Reputation: 2091

I write a demo for you, you can use it directly.

Here you go:

    public static List<V> listAllBlobs<T,V>(Expression<Func<T, V>> expression, string containerName)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=xxxx;AccountKey=xxxxx;BlobEndpoint=https://xxxxx.blob.core.windows.net/;QueueEndpoint=https://xxxxx.queue.core.windows.net/;TableEndpoint=https://xxxxxx.table.core.windows.net/;FileEndpoint=https://xxxxx.file.core.windows.net/;");

        CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName);
        container.CreateIfNotExists();
        var list = container.ListBlobs(useFlatBlobListing: true);

        List<V> data = list.OfType<T>().Select(expression.Compile()).ToList();
        return data;
    }

Usage and Screenshot:

enter image description here

Upvotes: 0

Jerry Liu
Jerry Liu

Reputation: 17800

Problem locates at b.Name.Uri.ToString(), Uri is the property of CloudBlockBlob instead of Name. You should remove Name like this b.Uri.ToString(). You probably are influenced by your parameter name blobNamesUri.

Upvotes: 1

Related Questions