ChevCast
ChevCast

Reputation: 59163

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
}

Upvotes: 2

Views: 2680

Answers (4)

Jeremy Meng
Jeremy Meng

Reputation: 469

in v12 you can retrieve metadata when listing blobs by passing the option includeMetadata: true

await containerClient.listBlobsFlat({ includeMetadata: true });

https://github.com/Azure/azure-sdk-for-js/blob/d2730549e078571df008e929f19c07aaf8f9efd9/sdk/storage/storage-blob/test/containerclient.spec.ts#L198

Upvotes: 1

// You can try this: 
for await (const blob of containerClient.listBlobsFlat()) {
    const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
    const meta = (await blockBlobClient.getProperties()).metadata;
    console.log(meta);
    // process metadata
}
  • I am assuming you have already declared blockBlobClient and containerClient.
  • If you haven't already declared blockBlobClient and containerClient ,then you can refer here

Upvotes: 3

George Chen
George Chen

Reputation: 14324

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

Upvotes: 1

Joey Cai
Joey Cai

Reputation: 20067

You can fetch the properties for a blob with the getBlobMetadata method.

var storage = require('azure-storage');
var blobService = storage.createBlobService();
var containerName = 'your-container-name';
var blobName = 'my-awesome-blob';
blobService.getBlobMetadata(containerName, blobName, function(err, result, response) {
    if (err) {
        console.error("Couldn't fetch metadata for blob %s", blobName);
        console.error(err);
    } else if (!response.isSuccessful) {
        console.error("Blob %s wasn't found container %s", blobName, containerName);
    } else {
        console.log("Successfully fetched metadata for blob %s", blobName);
        console.log(result.metadata);
    }
});

For more details, you could refer to this article.

Upvotes: 1

Related Questions