Allyl Isocyanate
Allyl Isocyanate

Reputation: 13626

Upload a file buffer using request promise to Amazon S3

I'm using the request-promise library. I'm trying to upload a file to a signed Amazon S3 URL:

var fs = require('fs')
var request = require('request-promise-native')
request({
  method: 'PUT',
  uri: 'https://MYBUCKET.amazonaws.com/myfile.pdf?AWSAccessKeyId=KEY&Expires=1489006131&Signature=SIGNATURE',
  headers: {
    'x-amz-server-side-encryption': 'AES256',
    'Content-Type': 'application/pdf'
  },
  file: fs.readFileSync('myfile.pdf')
}).then((r) => console.log('response', r)).catch((err) => console.log('err', err))

The request succeeds, but Amazon reports the file as 0 bytes in length.

Is the file parameter the correct argument?

Upvotes: 0

Views: 2893

Answers (1)

mscdex
mscdex

Reputation: 106736

I believe you want

body: fs.readFileSync('myfile.pdf')

instead of

file: fs.readFileSync('myfile.pdf')

Additionally, you can stream the file instead of buffering the entire file in memory first:

body: fs.createReadStream('myfile.pdf')

See the request documentation for more information.

Upvotes: 4

Related Questions