Reputation: 103
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!
Upvotes: 0
Views: 980
Reputation: 24138
Just a sample for you, there are two XML files in a virtual directory xml
of the container test
of my Azure Blob Storage, as the figure below.
If your expected result is [ 'xml/A.xml', 'xml/B.xml' ]
, you just need to use the Array.prototype.map()
method to get the list from data.entries
as the code below.
data.entries.map(entry => entry.name)
Here is my complete sample code.
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)
})();
The screenshot of the result is below.
Hope it helps.
Upvotes: 1
Reputation: 329
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);
}
Upvotes: 0