I almost have this working. I use angular-file-upload (http://nervgh.github.io/pages/angular-file-upload/examples/image-preview/) to upload a file to nodejs server and I pipe it through to Azure.
router.post('/storeimg', function (req, res, next) {
//create write stream for blob
var blobSvc = azure.createBlobService();
blobSvc.createContainerIfNotExists('images', function (error, result, response) {
if (!error) {
// Container exists and allows
// anonymous read access to blob
// content and metadata within this container
var stream = blobSvc.createWriteStreamToBlockBlob(
'images',
'test.png');
//pipe req to Azure BLOB write stream
req.pipe(stream);
req.on('error', function (error) {
//KO - handle piping errors
console.log('error: ' + error);
});
req.once('end', function () {
//OK
console.log('all ok');
});
}
});
});
The problem I have is that resulting file on Azure is prepended with the following
-----------------------------7df240321a0e9a
Content-Disposition: form-data; name="file"; filename="010218_osn.jpg"
Content-Type: image/jpeg
Which seems I also end up piping in the headers of the POST request. How do I avoid this? Is there a way to skip the headers?
UPDATE: Solution
var Busboy = require('busboy');
router.post('/storeimg', function (req, res, next) {
//create write stream for blob
var blobSvc = azure.createBlobService();
blobSvc.createContainerIfNotExists('images', function (error, result, response) {
if (!error) {
// Container exists and allows
// anonymous read access to blob
// content and metadata within this container
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
var stream = blobSvc.createWriteStreamToBlockBlob(
'images',
filename);
//pipe req to Azure BLOB write stream
file.pipe(stream);
});
busboy.on('finish', function () {
res.writeHead(200, { 'Connection': 'close' });
res.end("That's all folks!");
});
req.pipe(busboy);
req.on('error', function (error) {
//KO - handle piping errors
console.log('error: ' + error);
});
req.once('end', function () {
//OK
console.log('all ok');
});
}
});
});