Upload a binary file to S3 using AWS SDK for Node.js Upload a binary file to S3 using AWS SDK for Node.js node.js node.js

Upload a binary file to S3 using AWS SDK for Node.js


You don't need to convert the buffer to a base64 string. Just set body to data and it will work.


Here is a way to send a file using streams, which might be necessary for large files and will generally reduce memory overhead:

var AWS = require('aws-sdk'),    fs = require('fs');// For dev purposes onlyAWS.config.update({ accessKeyId: 'key', secretAccessKey: 'secret' });// Read in the file, convert it to base64, store to S3var fileStream = fs.createReadStream('myarchive.tgz');fileStream.on('error', function (err) {  if (err) { throw err; }});  fileStream.on('open', function () {  var s3 = new AWS.S3();  s3.putObject({    Bucket: 'mybucketname',    Key: 'myarchive.tgz',    Body: fileStream  }, function (err) {    if (err) { throw err; }  });});


I was able to upload my binary file this way.

var fileStream = fs.createReadStream("F:/directory/fileName.ext");var putParams = {    Bucket: s3bucket,    Key: s3key,    Body: fileStream};s3.putObject(putParams, function(putErr, putData){    if(putErr){        console.error(putErr);    } else {        console.log(putData);    }});