0

I'm trying to pipe a file from request.js straight to s3. The code below is the closest I've managed to figure out. It runs, but the file that ends up on s3 is zero bytes. What am I getting wrong?

var request = require('request');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();

request('https://placekitten.com/g/2000/2000')
    .on('response', function(response) {
        response.on('data', function(data) {
            // this is getting called
            console.log(data.length);
        });

        s3.upload({
            Body: response,
            Bucket: 'myBucket'
            Key: 'foo.jpg',
            // if ContentLength is set, the request just seems to hang?
            // ContentLength: parseInt(response.headers['content-length']),
        }, function(err, data) {
            // no error, but zero byte file
            console.log(err, data);
        });
    });

If there's an easier way using the native http/https modules, that would be fine too.

Josh Santangelo
  • 918
  • 3
  • 11
  • 22

1 Answers1

2

I'm not sure how to make it work with request.js, but the native HTTP modules seem to work.

var AWS = require('aws-sdk');
var https = require('https');
var s3 = new AWS.S3();

https.request('https://placekitten.com/g/2000/2000', function(response) {
    s3.upload({
        Body: response,
        Bucket: 'myBucket',
        Key: 'foo.jpg',
    }, function(err, data) {
        console.log(err, data);
    });
}).end();
Josh Santangelo
  • 918
  • 3
  • 11
  • 22