Node copy file to AWS S3

Published on:

Uses the nodejs AWS SDK to upload a file to S3.

Shows that if your EC2 instance has a role that allows write to S3 it will work even without ~/.aws/credentials and without passing credentials to new AWS.S3().

#!/usr/bin/env node

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

function _uploadToS3(bucket, key, body, cb) {
  var params = {
    Bucket: bucket,
    Key: key,
    Body: body,
    ACL: 'public-read'
  };
  s3.putObject(params, function(err, data) {
    if (err) {
      console.log(err);
    }
    var url = 'https://s3.amazonaws.com/' + bucket + '/' + key;
    cb(err, url);
  });
}


_uploadToS3('dev-quote-tool', 'jds-junk', 'some body', function(err, info) {
  console.log(err);
  console.log(info);
});