Last time we talked about downloading blobs from storage.
This time we’ll cover listing your existing containers and blobs.
Listing Containers in an Account
Listing the containers in a storage account is easy with the listContainerSegmented method.
123456789101112
varstorage=require('azure-storage');varblobService=storage.createBlobService();blobService.listContainersSegmented(null,function(err,result){if(err){console.log("Couldn't list containers");console.error(err);}else{console.log('Successfully listed containers');console.log(result.entries);console.log(result.continuationToken);}});
If you have enough containers, not all of them will be returned in one call.
If result.continuationToken is not null, there are more entries.
You can get the next segment of entries by calling listContainersSegmented again with result.continuationToken as the first result.
If you want to aggregate all containers, you can use the following code.
12345678910111213141516171819202122232425262728
varstorage=require('azure-storage');varblobService=storage.createBlobService();varcontainers=[];functionaggregateContainers(err,result,cb){if(err){cb(er);}else{containers=containers.concat(result.entries);if(result.continuationToken!==null){blobService.listContainersSegmented(result.continuationToken,aggregateContainers);}else{cb(null,containers);}}}blobService.listContainersSegmented(null,function(err,result){aggregateContainers(err,result,function(err,containers){if(err){console.log("Couldn't list containers");console.error(err);}else{console.log(containers);}});});
Listing Blobs in a Container
You can list blobs in a container with the listBlobsSegmented method.
12345678910111213
varstorage=require('azure-storage');varblobService=storage.createBlobService();varcontainerName='your-container-name';blobService.listBlobsSegmented(containerName,null,function(err,result){if(err){console.log("Couldn't list blobs for container %s",containerName);console.error(err);}else{console.log('Successfully listed blobs for container %s',containerName);console.log(result.entries);console.log(result.continuationToken);}});
As with containers, you can use the continuationToken to aggregate results.
varstorage=require('azure-storage');varblobService=storage.createBlobService();varcontainerName='your-container-name';varblobs=[];functionaggregateBlobs(err,result,cb){if(err){cb(er);}else{blobs=blobs.concat(result.entries);if(result.continuationToken!==null){blobService.listBlobsSegmented(containerName,result.continuationToken,aggregateBlobs);}else{cb(null,blobs);}}}blobService.listBlobsSegmented(containerName,null,function(err,result){aggregateBlobs(err,result,function(err,blobs){if(err){console.log("Couldn't list blobs");console.error(err);}else{console.log(blobs);}});});
Listing Blobs and Containers by Prefix
If you want to “query” blobs and containers by a prefix, you can do so with the listContainersSegmentedWithPrefix and listBlobsSegmentedWithPrefix methods.
123456789101112131415
varstorage=require('azure-storage');varblobService=storage.createBlobService();varprefix='images-';blobService.listContainersSegmentedWithPrefix(prefix,null,function(err,result){if(err){console.log("Couldn't list containers");console.error(err);}else{console.log("Found containers with prefix %s",prefix);console.log(result.entries);}});
1234567891011121314151617
varstorage=require('azure-storage');varblobService=storage.createBlobService();varcontainerName='your-container-name';varprefix='image-';blobService.listBlobsSegmentedWithPrefix(containerName,prefix,null,function(err,result){if(err){console.log("Couldn't list blobs");console.error(err);}else{console.log("Found blobs with prefix %s",prefix);console.log(result.entries);}});
Conclusion
In this post we covered listing blobs and containers in your storage account.
In the next post, we’ll cover blob metadata.
I am now accepting new clients for part-time consulting and software development projects.
Learn more