Node.js-删除Azure Blob

时间:2019-07-19 11:38:04

标签: node.js json azure azure-storage azure-storage-blobs

我试图使用以下方法仅从天蓝色blob容器中获取名称列表。目的是将数组传递给删除,下载等的不同方法。

listContainerBlobs = async (blobDirName) => {
    return new Promise((resolve, reject) => {

        const blobService = azureStorage.createBlobService(azureStorageConfig.accountName, azureStorageConfig.accountKey); 
        blobService.listBlobsSegmentedWithPrefix(`${azureStorageConfig.containerName}`, `${blobDirName}`, null, (err, data) => {    
            if (err) {
                reject(err);
            } else {
                var blobsArr = [];
                var blobsJSON = data.entries;
                for(var i = 0; i < blobsJSON.length; i++){
                    for(name in blobsJSON[i]){
                        if (blobsJSON[i] == "name") {
                            blobsArr.push(blobsJSON[i][key]);   
                        }
                      }
                }
                resolve(
                {
                    blobsArr
                });
            }
        });
    });
};

blobsArr始终返回为空。

以下是blobsJSON返回的JSON:

{
    "blobsJSON": [
        {
            "name": "WKRVAR000241/site_inst_files/avatar002.png",
            "creationTime": "Tue, 16 Jul 2019 22:49:22 GMT",
            "lastModified": "Tue, 16 Jul 2019 22:49:22 GMT",
            "etag": "0x8D70A3FD83B30DA",
            "contentLength": "5309",
            "contentSettings": {
                "contentType": "image/png",
                "contentEncoding": "",
                "contentLanguage": "",
                "contentMD5": "F1CkPOwHPwTMDf6a3t1yCg==",
                "cacheControl": "",
                "contentDisposition": ""
            },
            "blobType": "BlockBlob",
            "accessTier": "Hot",
            "accessTierInferred": true,
            "lease": {
                "status": "unlocked",
                "state": "available"
            },
            "serverEncrypted": "true"
        }
    ]
}

谁能告诉我我要去哪里错了。我只需要返回名称键的值列表。

谢谢!

2 个答案:

答案 0 :(得分:1)

仅为您一个示例,我的Azure Blob存储的容器xml的虚拟目录test中有两个XML文件,如下图。

enter image description here

如果预期结果为[ 'xml/A.xml', 'xml/B.xml' ],则只需使用Array.prototype.map()方法即可从data.entries获取列表,如下所示。

data.entries.map(entry => entry.name)

这是我完整的示例代码。

var azure = require('azure-storage');
const accountName = '<your account name>';
const accountKey = 'your account key';
const blobService = azure.createBlobService(accountName, accountKey);

const containerName = 'test';

listContainerBlobs = async (blobDirName) => {
    return new Promise((resolve, reject) => {

        //const blobService = azureStorage.createBlobService(azureStorageConfig.accountName, azureStorageConfig.accountKey); 
        blobService.listBlobsSegmentedWithPrefix(`${containerName}`, `${blobDirName}`, null, (err, data) => {    
            if (err) {
                reject(err);
            } else {
                resolve(data.entries.map(entry => entry.name))
            }
        });
    });
};

(async() => {
    blobs = await listContainerBlobs('xml/');
    console.log(blobs)
})();

结果的屏幕截图如下。

enter image description here

希望有帮助。

答案 1 :(得分:0)

        for(var i = 0; i < blobsJSON.length; i++){
            for(name in blobsJSON[i]){
                if (blobsJSON[i] == "name") {
                    blobsArr.push(blobsJSON[i][key]);   
                }
              }
        }

以上更新至:

        for(var i = 0; i < blobsJSON.length; i++){
            blobsArr.push(blobsJSON[i].name);   
        }