Azure Blobs with Node.js and Express

Monday, February 3, 2014

The Node.js packages for Azure are fun to work with. Here’s a little walkthrough for building an Express app to display the blobs in an Azure storage container.

1. Create an application with Express.

2. Install the Windows Azure Client Library for node (npm install azure)

3. When Express created the application, it should have added a route to send a request for the root of the site to routes.index. In other words, the app.js file will look like the following.

var express = require('express')
  , routes = require('./routes')
  , path = require('path');

var app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.get('/', routes.index);

app.listen(app.get('port'), function(){
  console.log('Listening on ' + app.get('port'));

4. The index function is where the code consumes the Azure API and connects to the blob service, fetches a list of all the blobs in a container, and renders a view to display the blobs.

var azure = require('azure');

exports.index = function(request, response) {

    var accessKey = 'yourKey';
    var storageAccount= 'yourName';
    var container = 'yourContainerName';

    var blobService = azure.createBlobService(storageAccount, accessKey);
    blobService.listBlobs(container, function(error, blobs) {        
        response.render('index', {
             error: error, 
             container: container, 
             blobs: blobs

You can pass the storage account name and the access key to createBlobService, or pass no parameters and define the environment variables AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY.

5. Finally, the Jade view to dump blob information into the browser.

extends layout

block content
    h1 Blob listing for #{container}

    if error
        h3= error

    if blobs
        h3= container
                th Name
                th Properties
            - each blob in blobs
                            - each value, name in
                                if value
                                    li= name + ":" + value

Coming soon – Node.js and Azure Table Storage

Comments are closed.