question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement passthrough blob streams (and support piping etc)

See original GitHub issue

TL;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);
});

https://github.com/Azure/azure-sdk-for-node/issues/1027

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:15

github_iconTop GitHub Comments

2reactions
saarycommented, Feb 1, 2014

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.

0reactions
christavcommented, Sep 13, 2014

Closing - see new azure-storage module.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Streams In NodeJS - Medium
Streams use internal Buffer to store the data while reading or writing. ... Here is an example of Passthrough Stream where it is...
Read more >
How to pipe multiple readable streams, from ... - Stack Overflow
Pipe multiple readable streams, received from multiple api requests, to a single writeable stream. The api responses are from ibm-watson's ...
Read more >
Stream | Node.js v19.3.0 Documentation
Simplified construction; Implementing a writable stream ... new PassThrough(); const writable = new Writable(); pass.pipe(writable); pass.unpipe(writable); ...
Read more >
minipass - npm
A very minimal implementation of a PassThrough stream · It's very fast for objects, strings, and buffers. Supports pipe()ing (including multi-pipe() and ...
Read more >
stream.PassThrough.pipe JavaScript and Node.js ... - Tabnine
How to use. pipe. function. in. PassThrough. Best JavaScript code snippets using stream.PassThrough.pipe(Showing top 15 results out of 315). Tabnine vs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found