Search code examples
javascriptnode.jsazureazure-storage

Get list of blobs in subfolder Azure


I am using the JS sdk to interact with Azure storage.

I have a hierarchy that looks like: demo (container name) / ID // list of files

So I am trying to get a list of files for a specific subfolder. I assumed using listBlobsByHierarchy was the one to use but I can not manage to get a list of blobs for that specific folder.

My code:

 for await (const item of containerClient.listBlobsByHierarchy("/")) {
        console.log(item, 'ITEM')
        if (item.kind === "prefix") {
          console.log(`\tBlobPrefix: ${item.name}`);
        } else {
          console.log(`\tBlobItem: name - ${item.name}, last modified - ${item.properties.lastModified}`);
        }
      }

which returns

    { kind: 'prefix', name: '5f09b7ab047f9804a861672d/' } ITEM
    BlobPrefix: 5f09b7ab047f9804a861672d/
{ kind: 'prefix', name: '5f300119536cc32ce44c0013/' } ITEM
    BlobPrefix: 5f300119536cc32ce44c0013/
{ kind: 'prefix', name: 'test/' } ITEM
    BlobPrefix: test/

Now I would like to get a list of files inside folder 5f09b7ab047f9804a861672d for example


Solution

  • with azure node storage sdk, we can use te method listBlobsFlat and listBlobsByHierarchy to list blobs. The two methods have some differences. The method listBlobsFlat can list all blobs. The method listBlobsByHierarchy just can just list top folder and blobs, which like you run "dir" under C:/. Besides, if you want to list sub blobs in the subfolder, we need to use prefix.

    For example

    My container structure is as below

    hello.txt
    ...
    test
      test.txt
      test1
         data.json 
    

    Code(list blobs in the folder test )

    const {
      BlobServiceClient,
      StorageSharedKeyCredential,
    } = require("@azure/storage-blob");
    
    // Enter your storage account name and shared key
    const account = "jimtestdiag924";
    const accountKey ="";
    
    // Use StorageSharedKeyCredential with storage account and account key
    // StorageSharedKeyCredential is only available in Node.js runtime, not in browsers
    const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
    const blobServiceClient = new BlobServiceClient(
      `https://${account}.blob.core.windows.net`,
      sharedKeyCredential,
    );
    const containerName = "testupload";
    async function listBlobs() {
      const containerClient = await blobServiceClient.getContainerClient(
        containerName,
      );
      console.log("list blobs with method listBlobsFlat");
      let iter = containerClient.listBlobsFlat({ prefix: "test/" });
      for await (const item of iter) {
        console.log(`\tBlobItem: name - ${item.name}`);
      }
      console.log("list blobs with method listBlobsByHierarchy");
      let iter1 = containerClient.listBlobsByHierarchy("/", { prefix: "test/" });
      for await (const item of iter1) {
        if (item.kind === "prefix") {
          console.log(`\tBlobPrefix: ${item.name}`);
          await listblob(containerClient, item.name);
        } else {
          console.log(`\tBlobItem: name - ${item.name}`);
        }
      }
    }
    
    async function listblob(containerClient, prefix) {
      let iter1 = containerClient.listBlobsByHierarchy("/", { prefix: prefix });
      for await (const item of iter1) {
        if (item.kind === "prefix") {
          console.log(`\tBlobPrefix: ${item.name}`);
        } else {
          console.log(`\tBlobItem: name - ${item.name}`);
        }
      }
    }
    
    listBlobs().catch((err) => {
      console.error("Error running sample:", err.message);
    });
    
    

    enter image description here