node.jsamazon-s3aws-sdkrequestjs

stream response from nodejs request to s3


How do you use request to download contents of a file and directly stream it up to s3 using the aws-sdk for node?

The code below gives me Object #<Request> has no method 'read' which makes it seem like request does not return a readable stream...

var req = require('request');
var s3 = new AWS.S3({params: {Bucket: myBucket, Key: s3Key}});
var imageStream = req.get(url)
    .on('response', function (response) {
      if (200 == response.statusCode) {
        //imageStream should be read()able by now right?
        s3.upload({Body: imageStream, ACL: "public-read", CacheControl: 5184000}, function (err, data) {  //2 months
          console.log(err,data);
        });
      }
    });
});

Per the aws-sdk docs Body needs to be a ReadableStream object.

What am I doing wrong here?

This can be pulled off using the s3-upload-stream module, however I'd prefer to limit my dependencies.


Solution

  • You want to use the response object if you're manually listening for the response stream:

    var req = require('request');
    var s3 = new AWS.S3({params: {Bucket: myBucket, Key: s3Key}});
    var imageStream = req.get(url)
        .on('response', function (response) {
          if (200 == response.statusCode) {
            s3.upload({Body: response, ACL: "public-read", CacheControl: 5184000}, function (err, data) {  //2 months
              console.log(err,data);
            });
          }
        });
    });