How can I list blobs in an Azure Storage container with Azure AD authentication using NodeJS?

hampton123 1,175 Reputation points
2023-08-07T20:30:44.22+00:00

I'm working on an API that communicates with an Azure Function which has a managed identity for storage blob manipulation. I want to list blobs in an Azure Storage container so that users of my API can select the file they want to download. I'm using NodeJS for my project. How can I achieve this using Azure AD authentication?

My current plan is to have the API send a list request to the Azure Function, which in turn will send the request URI https://myaccount.blob.core.windows.net/mycontainer?restype=container&comp=list to the Storage Account to get the list of files. However, I'm not sure if this is the best approach and would appreciate feedback on my strategy.

Let me know if more information is needed.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,529 questions
Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

Accepted answer
  1. Shweta Mathur 30,296 Reputation points Microsoft Employee Moderator
    2023-08-09T03:51:09.7966667+00:00

    Hi @hampton123 ,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others", I'll repost your solution in case you'd like to "Accept" the answer.

    As
    Hunter B
    mentioned, to list blobs in an Azure Storage container using NodeJS and Azure AD authentication, you can use the @azure/storage-blob package.

    const { DefaultAzureCredential } = require("@azure/identity");
    const { BlobServiceClient } = require("@azure/storage-blob");
    
    const account = "<account>";
    const defaultAzureCredential = new DefaultAzureCredential();
    
    const blobServiceClient = new BlobServiceClient(
      `https://${account}.blob.core.windows.net`,
      defaultAzureCredential
    );
    
    const containerName = "<container name>";
    
    async function main() {
      const containerClient = blobServiceClient.getContainerClient(containerName);
    
      let i = 1;
      let blobs = containerClient.listBlobsFlat();
      for await (const blob of blobs) {
        console.log(`Blob ${i++}: ${blob.name}`);
      }
    }
    
    main();
    
    
    

    This code creates a BlobServiceClient object using the managed identity of the Azure Function, gets a reference to the container you want to list blobs in, and then lists all blobs in the container using a for await...of loop.

    Thanks,

    Shweta

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. hampton123 1,175 Reputation points
    2023-08-08T17:26:23.2466667+00:00

    I completed this from my end - instead of using the previous link I stated, I actually used the code listed here. This allowed me to list blobs in a format I needed.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.