Search code examples
azureazure-functionsazure-blob-storageazure-node-sdk

How to access blob metadata using the Azure JavaScript SDK?


How do I read metadata for a blob in azure via the JavaScript SDK?

When I iterate the blobs returned from the specified container I see a metadata property:

enter image description here

But it's undefined, even though there is definitely metadata associated with the blob:

enter image description here

Is there something else I need to do to get the metadata to populate?

import { BlobServiceClient, SharedKeyCredential } from "@azure/storage-blob";

const account = "<redacted>";
const accountKey = "<redacted>";
const sharedKeyCredential = new SharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(`https://${account}.blob.core.windows.net`, sharedKeyCredential);
const containerClient = blobServiceClient.getContainerClient(podcastName);

const blobs = await containerClient.listBlobsFlat({ include: ["metadata"] });
for await (const blob of blobs) {
  console.log(blob.name);
  //blob.metadata is undefined
}

// package.json relevant dependencies
"dependencies": {
  "@azure/storage-blob": "^12.0.0-preview.2
}

Solution

  • I test it's null, then I use getProperties() to get the metadata and it worked, you could have a try.

    const containerName = "test";
    const blobName = "test.txt";
    
    
    let response;
    let marker;
    
    do {
        response = await containerURL.listBlobFlatSegment(aborter);
        marker = response.marker;
        for(let blob of response.segment.blobItems) {
    
            const url= BlockBlobURL.fromContainerURL(containerURL,blob.name);
            const pro=await url.getProperties(aborter);
    
            console.log(pro.metadata);
    
        }
    } while (marker);
    

    enter image description here