How to manage Microsoft Azure Blob Storage with Node.js

[Infragistics] Mihail Mateev / Friday, August 1, 2014

Node.js is one one of the the most popular growing platforms for development. The language, used with Node.js is JavaScript - he #1 most-used language on GitHub, and this trend is only going to increase (Forbes, 14 of July, 2014) : 

I started  several articles, describing some details how to use Node.js with Microsoft SQL Server ( you can see part 1 and part 2 ) and   Microsoft Azure Storage (the post about Azure Table Storage is available here) .   This blog is about how you can use Node.js and Microsoft Azure Blob Storage . 

You will learn how to start with Node and Blob Storage, covering the most often seen cases.

 

Microsoft Azure Blob Storage

Microsoft Azure storage services allow us to store/retrieve the NON RELATIONAL data to/from Windows Cloud environment. In Microsoft Azure Storage, the data can be stored in 4 different formats (v.i.z. Blobs, Tables and Queues, File Storage (in preview )). More details about what is Microsoft Azure Storage in general you can read in my previous post.

 

What is a Blob?

Azure Blob storage is a service for storing large amounts of unstructured data that can be accessed from anywhere in the world via HTTP or HTTPS. A single blob can be hundreds of gigabytes in size.

The Blob service contains the following components:

  • Storage Account: All access to Azure Storage is done through a storage account.

  • Container: A container provides a grouping of a set of blobs. All blobs must be in a container. An account can contain an unlimited number of containers. A container can store an unlimited number of blobs.

  • Blob: A file of any type and size. There are two types of blobs that can be stored in Azure Storage: block and page blobs. Most files are block blobs. A single block blob can be up to 200 GB in size. This tutorial uses block blobs. Page blobs, another blob type, can be up to 1 TB in size, and are more efficient when ranges of bytes in a file are modified frequently.

 

Blob stands for 'binary large object', which is an array of raw bytes. The following figure shows the hierarchical structure used for the Blob storage in Windows Azure:

 

Azure Storage Account can have multiple Containers. A Container can be considered as an array or collection of one or more Blobs. Also, each Blob can have one or more metadata properties to define what the Blob content is all about. The metadata properties are Name-Value collection of strings.

As mentioned before, the contents of every Blob on Microsoft Azure can be accessed by browsing its corresponding URI (REST). The URI is usually of the following format:

https://<Account>.blob.core.windows.net/<Container>/<BlobName>

 

How to use Blob Storage and Node.JS?

You can use different NodeJS packages to handle Azure Blob Storage. In this post we will cover azure Node package ( an official Microsoft Azure SDK ).

 

  • Microsoft Azure SDK for Node.js

The  official Microsoft Azure SDK (npm page:  https://www.npmjs.org/package/azure , homepage: http://github.com/WindowsAzure/azure-sdk-for-node  ) for Node.js.  provides a Node.js package that makes it easy to consume and manage Microsoft Azure Services.

The listed snippets below demonstrate how to manage Azure Blob service using Microsoft Azure SDK for Node.js.

 

  • Create an Azure Blob Service
   1: var azure = require('azure');
   2:  
   3: var accessKey = '[accountKey]';
   4: var storageAccount = '[accountName]';
   5: var containerName = 'nodejs';
   6:  
   7: //create a blob service set explicit credentials
   8: var blobService = azure.createBlobService(storageAccount, accessKey);

You can create Azure Blob Service using the environment variables to store the credentials  in the same way like for Azure Table service ( look at this post ).

 

  • List blobs in Azure Blob Storage

 

JavaScript code:

   1: var azure = require('azure');
   2:  
   3: exports.blobs = function (request, response) {
   4:  
   5:     var accessKey = '[accountKey]';
   6:     var storageAccount = '[accountName]';
   7:     var container = 'nodejs';
   8:  
   9:     var blobService = azure.createBlobService(storageAccount, accessKey);
  10:     //render blobs with blobs.jade view
  11:     blobService.listBlobs(container, function (error, blobs) {
  12:         response.render('blobs', {
  13:             error: error,
  14:             container: container,
  15:             blobs: blobs
  16:         });
  17:     });
  18: }

 

A jade view ( using Express.js )

   1: extends layout
   2:  
   3: block content
   4:     h1 Blob listing for #{container}
   5:  
   6:     if error
   7:         h3= error
   8:  
   9:     if blobs
  10:         h3= container
  11:         table
  12:             tr
  13:                 th Name
  14:                 th Properties
  15:             - each blob in blobs
  16:                 tr
  17:                     td= blob.name
  18:                     td
  19:                         ul
  20:                             - each value, name in blob.properties
  21:                                 if value
  22:                                     li= name + ":" + value

 

 

  • Upload file to Azure Blob Storage

One approach is to use multiparty node module. It is multipart/form-data parser which supports streaming.  Using Multiparty(npm install multiparty), a fork of Formidable, we can access the multipart data. This module will not stream the file to disk unless you tell it to.

   1: //upload a file to azure blob storage
   2: app.get('/upload', function (req, res) {
   3:     res.send(
   4:     '<form action="/upload" method="post" enctype="multipart/form-data">' +
   5:     '<input type="file" name="snapshot" />' +
   6:     '<input type="submit" value="Upload" />' +
   7:     '</form>'
   8: );
   9: });
  10:  
  11: app.post('/upload', function (req, res) {
  12:     var multiparty = require('multiparty');
  13:     var accessKey = '[accountKey]';
  14:     var storageAccount = '[accountName]';
  15:  
  16:     var container = 'nodejs';    
  17:     var blobService = azure.createBlobService(storageAccount, accessKey);
  18:     var form = new multiparty.Form();
  19:     
  20:     form.on('part', function (part) {
  21:         if (part.filename) {
  22:             
  23:             var size = part.byteCount - part.byteOffset;
  24:             var name = part.filename;
  25:             
  26:             blobService.createBlockBlobFromStream(container, name, part, size, function (error) {
  27:                 if (error) {
  28:                     res.send(' Blob create: error ');
  29:                 }
  30:             });
  31:         } else {
  32:             form.handlePart(part);
  33:         }
  34:     });
  35:     form.parse(req);
  36:     res.send('OK');
  37: });
  38: //end of upload a file to azure blob storage

 

Screens below demonstrate how to upload file to Azure Blob Storage using the sample demo  application.

 

 

  • Download files from Azure Blob Storage

To download the blob and write it to the file system, a similar getBlob or getBlobToFile  methods can be used.

 

The snipped below demonstrates how to use  getBlob:

   1: var azure = require('azure');
   2:  
   3: var blobService = azure.createBlobService();
   4:  
   5: //using getBlob to save a file from Azure Blob
   6: blobService.getBlob('[containerName]', '[blobName]').pipe(fs.createWriteStream('[myFileName]'));

 

Code below is part of the demo application, demonstrating how to download file from Azure Blob Storage using getBlobToFile :

   1:  
   2: //download azure blob storage cotent
   3: app.get('/downloadBlob', function (req, res) {
   4:     res.send(
   5:     '<form action="/downloadBlob" method="post" >' +
   6:     '<input type="text" name="blobFile" value="C:\\temp" />' +
   7:     '<input type="submit" value="Download" />' +
   8:     '</form>'
   9: );
  10: });
  11:  
  12: app.post('/downloadBlob', function (req, res) {
  13:     var fs = require('fs');
  14:     
  15:     if (!fs.existsSync) {
  16:         fs.existsSync = require('path').existsSync;
  17:     }
  18:     var destinationDirectoryPath = req.body.blobFile;
  19:     var accessKey = '[accountKey]';
  20:     var storageAccount = '[accountName]';
  21:     var containerName = 'nodejs';
  22:  
  23:     var blobService = azure.createBlobService(storageAccount, accessKey); //ok
  24:     
  25:     if (!fs.existsSync(destinationDirectoryPath)) {
  26:         console.log(destinationDirectoryPath + ' is an invalid directory path.');
  27:     } else {
  28:         downloadFilesParallel(res, blobService, containerName, destinationDirectoryPath);
  29:     }
  30:  
  31: });

 

downloadFilesParallel function, used in the code above:

   1: function downloadFilesParallel(res, blobService, containerName, destinationDirectoryPath) {
   2:     blobService.listBlobs(containerName, function (error, blobs) {
   3:         if (error) {
   4:             console.log(error);
   5:         } else {
   6:             var blobsDownloaded = 0;
   7:             res.writeHead(200, { 'Content-Type': 'text/plain' })
   8:             blobs.forEach(function (blob) {
   9:                 blobService.getBlobToFile(containerName, blob.name, destinationDirectoryPath + '/' + blob.name, function (error2) {
  10:                     blobsDownloaded++;
  11:                     
  12:                     if (error2) {
  13:                         console.log(error2);
  14:                     } else {
  15:                         res.write('\nBlob ' + blob.name + ' download finished.');
  16:                         
  17:                         if (blobsDownloaded === blobs.length) {
  18:                             // Wait until all workers complete and the blobs are downloaded
  19:                             res.end('\nAll files downloaded');
  20:                         }
  21:                     }
  22:                 });
  23:             });
  24:         }
  25:     });
  26: }

 

Screenshots from the sample app, demonstrating how to download a file from Blob Storage.

 

 

 

Specialists can learn much more details about Azure Blob services and Node.  Microsoft continues to grow and improve blob storage. This post describes how to start, covering the base cases and most popular Node.js modules for Azure Blob Storage. It will be useful for both – JavaScript developers who don’t have experience with Microsoft Azure and Azure developers who have less experience with JavaScript and/or Node.js.

Developers on the Microsoft Azure Platform should become familiar with how Azure Blob Storage works and how to handle unstructured data in the Microsoft cloud. Knowing how blob storage works will help you determine if it is a good fit for your particular requirements.

 

You can download source code from Git repository .

  

If you want more information about how to use Microsoft Azure Storage  & Node.js feel free to contact me at mmateev@infragistics.com

You can learn more about Node.js , Microsoft Azure and related events like Azure Bootcamp Bulgaria if you follow us on Twitter @mihailmateev  and @Infragistics and stay in touch on Facebook, Google+, LinkedIn and Infragistics Friends User Group !