Search code examples
node.jsjsonazureazure-storageazure-blob-storage

Node.js - Delete Azure blob


I'm trying to get a list of names only from an azure blob container with the following method. The aim is to pass an array to different methods which delete, download etc.

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 is always returned empty.

Below is the JSON returned by blobsJSON:

{
    "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"
        }
    ]
}

Can anyone tell me where I'm going wrong. I need to return a list of values for the name key only.

Thanks!


Solution

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

    Update above to:

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