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

How to stream a Windows Azure Blob to the client using Node.js?

How to stream a Windows Azure Blob to the client using Node.js?

Problem

I would like to send a Windows Azure Blob (an image) directly to the client ; I am trying this :

    blobService.getBlobToStream('images', req.url, res, function(err, blob) {
    if (!err) {
        res.writeHead(200, { 'Content-Type': blob.contentType });
    } else {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end(err);
    }
});

I am trying to pipe the Blob stream directly to the response stream ; should this work ? In Firefox, I get a message : "The image cannot be displayed because it contains errors". Looking at Firebug, the size of the image is 0.

Problem courtesy of: tomconte

Solution

This seems to work prefectly for me (if this is any help?):

var azure = require('azure');
var http = require('http');
http.createServer(function (req, res) {
    var blobService = azure.createBlobService("xxx", "yyy", "blob.core.windows.net").withFilter(new azure.ExponentialRetryPolicyFilter());
    blobService.getBlobToStream('container', 'image.png', res, function(error){
        if(!error){
            res.writeHead(200, {'Content-Type': 'image/png'});
            res.end();
        }
        else
        {
            console.log('error');
            console.log(error);
            res.end();
        }
    });
}).listen(8080, "127.0.0.1");

Update

Just figured it out. req.url will have a leading slash (/). Which I'm guessing will not match your image filename.

Solution courtesy of: Richard Astbury

Discussion

View additional discussion.



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

Share the post

How to stream a Windows Azure Blob to the client using Node.js?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×