Azure Blob Storage and Node: Listing Blobs

This is part of a series on working with Azure Blob Storage in Node.

  1. Introduction
  2. First Steps
  3. Creating Blobs
  4. Downloading Blobs
  5. Listing Blobs
  6. Blob Metadata
  7. All Together

Listing Containers and Blobs in Storage

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.

1
2
3
4
5
6
7
8
9
10
11
12
var storage = require('azure-storage');
var blobService = 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var storage = require('azure-storage');
var blobService = storage.createBlobService();

var containers = [];
function aggregateContainers(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.

1
2
3
4
5
6
7
8
9
10
11
12
13
var storage = require('azure-storage');
var blobService = storage.createBlobService();
var containerName = '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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var storage = require('azure-storage');
var blobService = storage.createBlobService();
var containerName = 'your-container-name';

var blobs = [];
function aggregateBlobs(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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var storage = require('azure-storage');
var blobService = storage.createBlobService();
var prefix = '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);
        }
    });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var storage = require('azure-storage');
var blobService = storage.createBlobService();
var containerName = 'your-container-name';
var prefix = '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

I haven't configured comments for this blog, but if you want to get in touch, you can find me on Twitter