ginad
ginad

Reputation: 1973

Is it possible to identify the length / size of a readable stream?

I'm trying to resize a file in node using gm.js and use the .stream() to create a readable stream of the resized image. Now I want to upload it using knox.js .putStream() but Content-Length is a required header. Is it possible to identify the size of the readable stream so I can use it in the Content-Length header?

Thanks in advance guys.

Upvotes: 1

Views: 5229

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51450

If your files aren't too large, you could bufferize your stream using raw-body module before uploading it to S3:

var rawBody = require('raw-body');
var knox = require('knox');

function putStream(stream, filepath, headers, next) {
  rawBody(stream, function(err, buffer) {
    if (err) return next(err);
    headers['Content-Length'] = buffer.length;
    knox.putBuffer(buffer, filepath, headers, next);
  });
};

If your files are extremely large, it may be better to use mscdex's solution with knox-mpu module.

Upvotes: 1

Related Questions