aws-sdk-js: Can't upload a http.IncomingMessage stream from request

If using native Node.js, this works:

var https = require('https')
var AWS = require('aws-sdk')

var s3 = new AWS.S3()
var url = 'https://a0.awsstatic.com/main/images/logos/aws_logo_105x39.png'

console.log('Requesting', url)
https.get(url, function (res) {
  var opts = {
    Bucket: 'name-of-bucket',
    Key: 'test.png',
    Body: res,
    ContentType: res.headers['content-type'],
    ContentLength: res.headers['content-length']
  }

  console.log('Starting upload to S3')
  s3.upload(opts, function (err) {
    if (err) throw err
    console.log('Upload completed')
  })
})

Using the request npm module, you should be able to do the same thing:

var request = require('request')
var AWS = require('aws-sdk')

var s3 = new AWS.S3()
var url = 'https://a0.awsstatic.com/main/images/logos/aws_logo_105x39.png'

console.log('Requesting', url)
request(url).on('response', function (res) {
  var opts = {
    Bucket: 'name-of-bucket',
    Key: 'test.png',
    Body: res,
    ContentType: res.headers['content-type'],
    ContentLength: res.headers['content-length']
  }

  console.log('Starting upload to S3')
  s3.upload(opts, function (err) {
    if (err) throw err
    console.log('Upload completed')
  })
})

But this will never complete the upload. Instead the S3 connection times out after waiting for a while.

If I change the s3.upload to s3.putObject then everything works fine?!

The res object provided to the response event is in both cases an instance of http.IncomingMessage, so everything should be the same - but apparently it isn’t. Could it be that you are relying on some Node.js internals that is different when the http.IncomingMessage object doesn’t come directly from the http / https module?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 2
  • Comments: 21 (6 by maintainers)

Most upvoted comments

Can anybody from the AWS team or the @aws or @request team comment on this?