Search code examples
javascriptazure-storageazure-blob-storageazure-container-service

Get azure container blobs list by directory


I'm organizing my container content by directories

rep1/subrep1/subsubrep1'
rep2/subrep2/subsubrep2'

inside each rep I have blob files.

I would like to retrieve blobs list of `rep1/subrep1/subsubrep1'

the container called tenants.

try {
    const blobServiceClient = await BlobServiceClient.fromConnectionString(
        process.env.AzureWebJobsStorage
    );

    const containerClient = await blobServiceClient.getContainerClient(
        'tenants'
    );
    for await (const blob of containerClient.listBlobsFlat()) {
        let blobClient = await containerClient.getBlobClient(blob.name);
        let splitedString = blobClient.name.split('/')
        filesList.push({
            label: splitedString[splitedString.length].split(".wav")[0],
            value: blobClient.url
        });
    }
} catch (err) {
    console.log(err)
}

Actually with my code I should iterate all blob files.

I can filter then the result but I would find how to get a specific subfolder blobs


Solution

  • Instead of using listBlobsFlat, use listBlobsByHierachy. For delimiter parameter, specify an empty string and specify rep1/subrep1/subsubrep1 as prefix in options parameter.

    Something like:

    let iter = await containerClient.listBlobsByHierarchy("", { prefix: "rep1/subrep1/subsubrep1/" });