Azure Blob Storage and Node: Downloading 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

Downloading From Blob Storage

Last time we talked about creating Azure blobs. This time we’ll turn things around and talk about downloading Azure blobs.

Account Connection Boilerplate

As stated in the previous post, to work with blob storage, you first need to create a client instance. To avoid duplicating code over and over, we’ll do that once here, and assume it in the other code samples.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var storage = require('azure-storage');
var blobService = storage.createBlobService();
var containerName = 'your-container-name';
blobService.createContainerIfNotExists(
    containerName,
    function(err, result, response) {
        if (err) {
            console.log("Couldn't create container %s", containerName);
            console.error(err);
        } else {
            if (result) {
                console.log('Container %s created', containerName);
            } else {
                console.log('Container %s already exists', containerName);
            }

            // Your code goes here
        }
    });

Again, for the rest of the code samples, we’ll assume that they’re replacing the “Your code goes here” placeholder in the code above.

Checking if a Blob Exists

In some cases, you don’t need a blob’s contents, you just need to know if it exists. You can do this with the getBlobProperties method.

1
2
3
4
5
6
7
8
9
10
11
var blobName = 'my-nonexistent-blob'
blobService.getBlobProperties(
    containerName,
    blobName,
    function(err, properties, status) {
        if (status.isSuccessful) {
            // Blob exists
        } else {
            // Blob doesn't exist
        }
    });

Downloading a Blob As Text

To download a blob as a text string, use the getBlobToText method.

1
2
3
4
5
6
7
8
9
10
11
12
13
var blobName = 'my-awesome-text-blob';
blobService.getBlobToText(
    containerName,
    blobName,
    function(err, blobContent, blob) {
        if (err) {
            console.error("Couldn't download blob %s", blobName);
            console.error(err);
        } else {
            console.log("Sucessfully downloaded blob %s", blobName);
            console.log(blobContent);
        }
    });

Downloading a Blob to a File

To download a blob to a file, use the getBlobToFile method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var fs = require('fs');
var fileName = 'hello-world.txt';
var blobName = 'my-awesome-file-blob';
blobService.getBlobToFile(
    containerName,
    blobName,
    fileName,
    function(err, blob) {
        if (err) {
            console.error("Couldn't download blob %s", blobName);
            console.error(err);
        } else {
            console.log("Sucessfully downloaded blob %s to %s", blobName, fileName);
            fs.readFile(fileName, function(err, fileContents) {
                if (err) {
                    console.error("Couldn't read file %s", fileName);
                    console.error(err);
                } else {
                    console.log(fileContents);
                }
            });
        }
    });

Downloading a Blob to a Stream

To download a blob to a stream, use the getBlobToStream method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// For simplicity, assume getSomeStream returns a writable stream
var myStream = getSomeStream();
var blobName = 'my-awesome-stream-blob';
blobService.getBlobToStream(
    containerName,
    blobName,
    myStream,
    function(err, blob) {
        if (err) {
            console.error("Couldn't download blob %s", blobName);
            console.error(err);
        } else {
            console.log("Sucessfully downloaded blob %s", blobName);
        }
    });

You can also integrate with a web server to enable downloads from blob storage. This is powerfull when combined with the getBlobProperties method.

In the following example, we use the createReadStream method so we can easily pipe the stream to the server response object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
app.get('/download/:file', function(req, res) {
    var fileName = req.params.file;
    blobService.getBlobProperties(
        containerName,
        fileName,
        function(err, properties, status) {
            if (err) {
                res.send(502, "Error fetching file: %s", err.message);
            } else if (!status.isSuccessful) {
                res.send(404, "The file %s does not exist", fileName);
            } else {
                res.header('Content-Type', properties.contentType);
                blobService.createReadStream(containerName, fileName).pipe(res);
            }
        });
});

Conclusion

In this post we covered how to check whether a blob exists, and how to download the blob. In the next post, we’ll cover how to list containers and blobs.

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