Implement passthrough blob streams (and support piping etc)
See original GitHub issueTL;DR
The blobService.getBlobToStream
method takes a writable stream, which is not how node.js normally works. It also seems to delay giving you the response object until after the download has completed. This means you can’t easily stream a file to the client while setting appropriate response headers first.
TL;DR
The getBlobToStream
API seems backwards to how virtually the entire rest of the node.js eco system works. I’m finding it really difficult to work out exactly what goes on.
I would expect it to look something like:
var blobService = azure.createBlobService();
// blob service should directly return a readable stream
// that emits a `response` event before any data
var stream = blobService.getBlobToStream('taskcontainer', 'task1');
stream.on('error', function (err) {
// there was an error reading blob
});
stream.on('response', function (res) {
if (res.statusCode >= 400) {
//there was an error reading blob
} else {
res.pipe(fs.createWriteStream('task1-download.txt'));
}
});
Because the result is a readable stream, you could skip the handling for the response and error events (if you don’t mind your app just crashing):
var blobService = azure.createBlobService();
// blob service should directly return a readable stream
// that emits a `response` event before any data
blobService.getBlobToStream('taskcontainer', 'task1')
.pipe(fs.createWriteStream('task1-download.txt'));
It’s also customary to have an optional callback that gets subcribed to both error
and response
events:
var blobService = azure.createBlobService();
blobService.getBlobToStream('taskcontainer', 'task1', function (err, res) {
if (err || res.statusCode >= 400) {
//somethign went wrong
} else {
res.pipe(fs.createWriteStream('task1-download.txt'));
}
});
Internally this would do:
stream.once('error', callback);
stream.on('response', function (res) {
stream.removeListener('error', callback);
callback(null, res);
});
Issue Analytics
- State:
- Created 10 years ago
- Comments:15
Top GitHub Comments
Hi,
Is there a way to set the headers on the response stream? I would like to proxy the blob storage stream to a secondary response. I would like to also proxy the relevant headers like: Content-Type, ETag, Md5, and the property headers.
I could find a reasonable way to achieve this.
Closing - see new azure-storage module.