1

如何通过 JavaScript SDK 在 azure 中读取 blob 的元数据?

当我迭代从指定容器返回的 blob 时,我看到一个元数据属性:

在此处输入图像描述

但它是未定义的,即使肯定有与 blob 关联的元数据:

在此处输入图像描述

我还需要做些什么来填充元数据吗?

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
}
4

3 回答 3

1

getBlobMetadata您可以使用该方法获取 blob 的属性。

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);
    }
});

更多细节,你可以参考这篇文章

于 2019-09-03T06:17:51.670 回答
1

我测试它是否为空,然后我使用getProperties()来获取元数据并且它有效,你可以尝试一下。

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);

在此处输入图像描述

于 2019-09-03T08:17:51.037 回答
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
}
  • 我假设您已经声明了blockBlobClientand containerClient
  • 如果你还没有声明blockBlobClientand containerClient,那么你可以参考这里
于 2021-11-20T11:26:31.100 回答