Last active
December 2, 2018 21:46
-
-
Save simonwhatley/e14b7e2c72dc327b8c00f26f3937db40 to your computer and use it in GitHub Desktop.
Uploading files to AWS S3 using NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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