【问题标题】:Node.js - Delete Azure blobNode.js - 删除 Azure blob
【发布时间】:2019-11-28 09:45:53
【问题描述】:

我正在尝试使用以下方法仅从 azure 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
                });
            }
        });
    });
};

blobArr 总是返回空。

下面是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"
        }
    ]
}

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

    只是一个示例,在我的 Azure Blob Storage 的容器 test 的虚拟目录 xml 中有两个 XML 文件,如下图。

    如果您的预期结果是[ '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)
    })();
    

    结果截图如下。

    希望对你有帮助。

    【讨论】:

    • 感谢@Peter Pan,这有助于并且似乎是一种更清洁的方式。感谢您抽出宝贵时间提供此信息。
    【解决方案2】:
            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);   
            }
    

    【讨论】:

      猜你喜欢
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多