Metaphor
Metaphor

Reputation: 6405

How is the prefix parameter used in CloudBlobContainer.ListBlobs() to get files from a virtual folder in Azure blob storage

I'm trying to get a listing in a single virtual folder in Azure blob storage. The files are organized in /{container}/{classification}/{title} folder structure, with all files in "title" virtual folders.

This is the function I use that works with no prefix but fails to return any results when I supply a prefix.

public static List<string> List(string classification, string title, StorageAccount sa)
{
    List<string> fileList = new List<string>();
    CloudBlobContainer container = GetBlobContainer(sa);
    var prefix = $"/{container.Name}/{classification}/{title}/";
    Console.WriteLine(prefix);

    var list = container.ListBlobs(prefix, useFlatBlobListing: true);

    foreach (var blob in list)
    {
        var blobFileName = blob.Uri.AbsolutePath;
        fileList.Add(blobFileName);
    }

    return fileList;
}

Upvotes: 2

Views: 3453

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136226

You don't need to include container name in the prefix. Please change following line of code:

var prefix = $"/{container.Name}/{classification}/{title}/";

to:

var prefix = $"{classification}/{title}/";

And this will list all blobs names of which starting with that prefix.

Upvotes: 3

Related Questions