Skip to content

Instantly share code, notes, and snippets.

@simonwhatley
Last active December 2, 2018 21:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwhatley/e14b7e2c72dc327b8c00f26f3937db40 to your computer and use it in GitHub Desktop.
Save simonwhatley/e14b7e2c72dc327b8c00f26f3937db40 to your computer and use it in GitHub Desktop.
Uploading files to AWS S3 using NodeJS
const AWS = require('aws-sdk');
const fs = require('fs');
const path = require('path');
//configuring the AWS environment
AWS.config.update({
accessKeyId: "AWS_ACCESS_KEY",
secretAccessKey: "AWS_SECRET_ACCESS_KEY"
});
let s3 = new AWS.S3();
let filePath = "./data/file.txt";
//configuring parameters
let params = {
Bucket: "AWS_BUCKET_NAME",
Body: fs.createReadStream(filePath),
Key: "folder/" + Date.now() + "_" + path.basename(filePath)
};
s3.upload(params, (err, data) => {
//handle error
if (err) {
console.log("Error:", err);
}
//success
if (data) {
console.log("Uploaded to:", data.Location);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment