Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

node.js azure storage blob

Tags: blob azure nodejs

node.js azure storage blob

Problem

node.js beginner. I tried to exec file upload sample below on node.js, but it does not work at blob.client.createContainerIfNotExists().

Error displays:

Error: createContainerIfNotExists

It means that error case of createContainerIfNotExists() displays simply.

I wonder if I mistake how to write blob.client.createContainerIfNotExists() or, azure.createBlobService() could not success.

node.js version v0.6.12
express version 2.5.11
azure version 0.5.3

Thank you!

/**********************/
  File upload sample:
/**********************/

var DEVSTORE_STORAGE_ACCOUNT = 'xxxxx';
var DEVSTORE_STORAGE_ACCESS_KEY= 'xxxx';
var DEVSTORE_BLOB_HOST = 'xxxxx';

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

var util   = require('util');

// Azure module
var azure  = require('azure');
var blob   = require('./blob.js');


// BLOB container
blob.CONTAINER = 'nodejs';

// BLOB service
 blob.client = azure.createBlobService(
  DEVSTORE_STORAGE_ACCOUNT,
  DEVSTORE_STORAGE_ACCESS_KEY,
  DEVSTORE_BLOB_HOST);

var app = module.exports = express.createServer();

 // Configuration

app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  // app.use(express.logger());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

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

// BLOB upload
app.get('/upload', routes.upload);

// BLOB upload
app.post('/uploadtoblob', routes.uploadblob);

// BLOB list
app.get('/list', routes.listblobs);

// BLOB delete
app.post('/delete/:id', routes.deleteblob);

// BLOB property
app.get('/info/:id', routes.information);

// BLOB container create
blob.client.createContainerIfNotExists(blob.CONTAINER, function(err) {
  if (err) {
    console.log('Error : createContainerIfNotExists');
    process.exit(1);
  } else {

    blob.client.setContainerAcl(blob.CONTAINER,         azure.Constants.BlobConstants.BlobContainerPublicAccessType.BLOB, function(err) {
      if(err) {
    console.log('Error : setContainerAcl');
    process.exit(1);
      }
    });
  }
});

var port = process.env.port || 3000;
app.listen(port, function(){
  console.log("Express server listening on port %d in %s mode", app.address().port,     app.settings.env);
});
Problem courtesy of: otogen

Solution

The latest azure sdk for node requires node version > 0.6.l5. I would suggest upgrading both the sdk and your node version.

You can change your code as follows to get more information on the specific error:

console.log('Error : createContainerIfNotExists' + JSON.stringify(err));

You need to eitehr have the azure storage emulator runnign locally or you need to provide credentials for an azure storage account that the app can use. The is usually done through the AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY environment variables, or by passing in a connection string to the createBlobService factory.

For step-by-step samples on setting up your first application using blob storage, see:

http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/blob-storage/

Solution courtesy of: Mark Cowlishaw - MSFT

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

node.js azure storage blob

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×