Created
December 14, 2019 21:33
-
-
Save james-fourth/e4dfc76d5ecc56d60f49ff7bbcd8be58 to your computer and use it in GitHub Desktop.
AWS-SDK S3 upload
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
// Load the AWS SDK, fs and path modules for Node.js | |
import AWS from "aws-sdk"; | |
import fs from "fs"; | |
import path from "path"; | |
// Set the region | |
AWS.config.update({region: "us-west-1"}) | |
// Create S3 service object | |
const s3 = new AWS.S3({apiVersion: "2006-03-01"}); | |
// Call S3 to retrieve upload file to specified bucket | |
let uploadParams = {Bucket: process.argv[2], Key: '', Body: ''}; | |
const file = process.argv[3]; | |
// Configure the file stream and obtain the upload parameters | |
const fileStream = fs.createReadStream(file); | |
fileStream.on('error', function(err) { | |
console.log('File', err); | |
}); | |
uploadParams.Body = fileStream; | |
uploadParams.Key = path.basename(file); | |
let myLocation; | |
// Call S3 to retrieve upload file to specified bucket | |
s3.upload (uploadParams, function(err, data) { | |
if (err) { | |
console.log(err); | |
} else { | |
const uploadLocation = data.Location; | |
console.log("Upload success", data.Location); | |
} | |
}); | |
// setTimeout(() => { | |
// console.log(myLocation); | |
// }, 5000); | |
export default myLocation; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment