2

I've reviewed this: Getting list of names of Azure blob files in a container?

and this: https://feedback.azure.com/forums/287593-logic-apps/suggestions/16252474-list-files-in-folder-on-azure-blob-storage

But I have not found any code examples of how to list the files in a particular virtual folder within a container using c#. This is as far as I got. I do not see any way to specify a file path in the ListBlobs ()method.

        var blobStorageAccount = GetStorageAccount();
        var blobClient = blobStorageAccount.CreateCloudBlobClient();
        var blobContainer = blobClient.GetContainerReference(containerName);
        List<string> blobNames = blobContainer.ListBlobs().OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
E. A. Bagby
  • 824
  • 9
  • 24

2 Answers2

5

There is no such thing as a subfolder. You have containers containing blobs. You do have virtual folders, for example

/container/virtualfolder/myblob

The container name is container and the blob name is virtualfolder/myblob

You can list all blobs in a virtual folder using the prefix parameter (see the docs):

        var blobStorageAccount = GetStorageAccount();
        var blobClient = blobStorageAccount.CreateCloudBlobClient();
        var blobContainer = blobClient.GetContainerReference(containerName);
        List<string> blobNames = blobContainer.ListBlobs(prefix: "virtualfolder").OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • That worked. Thanks! Is there a built in way to isolate the blob name from the virtual path? Or, perhaps Path.GetFileName() will work – E. A. Bagby Jul 30 '18 at 18:42
  • 1
    Hmm not sure. `Path.GetFileName()` might work, or you can try a `.Split('/').Last()` – Peter Bons Jul 30 '18 at 18:48
1

Microsoft.Azure.Storage.Blob is deprecated. Using Azure.Storage.Blob looks like it would be:

List<string> blobNames = blobContainer.GetBlobs(prefix: "virtualfolder").Select(b => b.Name).ToList();
Kirby
  • 15,127
  • 10
  • 89
  • 104