Download an Azure blob container subfolder to a listview

1 Answer 21 Views
ListView
Scott
Top achievements
Rank 1
Scott asked on 03 Apr 2025, 04:51 PM
I have the listview code for downloading the blob container. But I need to only get a subfolder in the container, not the whole thing. Is there a way to add the variable for the subfolder or should I structure the data in Azure differently and create a new container instead of using subfolders in one container?
public static async Task<List<string>> GetUploadedImages(AzureStorageConfig _storageConfig)
{
    List<ImageViewModel> images = new List<ImageViewModel>();
// Create BlobServiceClient from the account URI BlobServiceClient blobServiceClient = new BlobServiceClient(_storageConfig.ConnectionString); // Get reference to the container BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ImageContainer); if (container.Exists()) { var data = container.GetBlobs(); foreach (BlobItem blobItem in container.GetBlobs()) { images.Add(container.Uri + "/" + blobItem.Name); } } return await Task.FromResult(images); }

James
Top achievements
Rank 1
commented on 04 Apr 2025, 06:05 PM | edited

In the GetBlobsAsync method there should be a prefix parameter which you can use like a path for the folder you're targeting, have you tried this?

 

var Container = await GetCloudBlobClientAsync();
await foreach (BlobItem blob in Container.GetBlobsAsync(BlobTraits.None, BlobStates.None, path))
{
  //do something with your blob
}
Scott
Top achievements
Rank 1
commented on 05 Apr 2025, 01:47 PM

Thank you James, that is what I needed.

1 Answer, 1 is accepted

Sort by
0
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
answered on 05 Apr 2025, 10:37 AM

Hi Scott,

Yes, you can retrieve only a specific "subfolder" from a blob container by leveraging Azure Storage's virtual directory concept. While Azure Blob Storage uses a flat namespace, you can simulate folders by including a delimiter (typically a /) in the blob names. This lets you organize and access blobs in a hierarchical manner.

To list blobs under a specific virtual directory (or "subfolder"), you can use the hierarchical listing approach and specify a prefix in your request corresponding to the subfolder path. This way, you're not required to create a separate container—just structure your blob names using slashes to mimic folders.

Here's a detailed example from the Azure docs: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blobs-list#use-a-hierarchical-listing


Regards,
Anislav Atanasov

Tags
ListView
Asked by
Scott
Top achievements
Rank 1
Answers by
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Share this question
or