Last active
May 5, 2021 19:45
-
-
Save anushshukla/e054ac900950227489b0cffca4e12fed to your computer and use it in GitHub Desktop.
S3 Util to Upload files and fetch uploaded files
This file contains hidden or 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 fs = require('fs'); // npm i fs | |
| const AWS = require('aws-sdk'); // npm i aws-sdk | |
| const arguments = {}; | |
| process.argv.forEach(arg => { | |
| const [index, value] = arg.split("="); | |
| console.log(index, value); | |
| arguments[index] = value; | |
| }); | |
| AWS.config.update({ | |
| accessKeyId: "<AWS-ACCESS-KEY>", | |
| secretAccessKey: "<AWS-SECRET-KEY>" | |
| }); | |
| const s3 = new AWS.S3({ region: "<AWS-S3-REGION>" }); | |
| const getParams = () => { | |
| const filepath = '/Users/system/Pictures/f0d33d80-0adc-11ea-8755-d33e3212dd94.png'; | |
| const [ fileName, fileExt ] = filepath.substring(filepath.lastIndexOf('/') +1).split('.'); | |
| const Body = fs.createReadStream(filepath); | |
| const now = new Date(); | |
| const timeStamp = now.getTime(); | |
| const Key = `${fileName}_${timeStamp}.${fileExt}`; | |
| // to set meta data of the file for showing the file inline instead of force download | |
| // const ContentType = 'image/png'; | |
| // const ContentType = 'text/csv'; | |
| const ContentType = 'text/plain'; | |
| // const ACL = 'public-read'; | |
| const params = { | |
| Bucket = "<AWS-S3-BUCKET>", | |
| Key, | |
| Body, | |
| ACL, | |
| ContentType, | |
| }; | |
| return params; | |
| } | |
| const getUploadLink = () => { | |
| const minuteInSeconds = 60; | |
| const hourInSeconds = 60 * minuteInSeconds; | |
| const dayInSeconds = 24 * hourInSeconds; | |
| const weekInSeconds = 7 * dayInSeconds; | |
| const Expires = 1 * weekInSeconds; | |
| const ACL = 'public-read'; | |
| const ContentType = 'image/png'; | |
| const Key = getParams().Key | |
| const params = { | |
| Key, | |
| Bucket, | |
| Expires, | |
| ContentType, | |
| ACL | |
| } | |
| s3.getSignedUrl('putObject', params, function(err, data) { | |
| if (err) console.log('getLink error ->', err); | |
| else console.log('getLink data ->', data); | |
| }); | |
| } | |
| const upload = () => { | |
| const params = getParams(); | |
| s3.upload(params, function(err, data) { | |
| if (err) console.log('upload error ->', err); | |
| else console.log('upload data ->', data); | |
| }); | |
| } | |
| const putObject = () => { | |
| const params = getParams(); | |
| s3.putObject(params, function(err, data) { | |
| if (err) console.log('putObject error ->', err); | |
| else console.log('putObject data ->', data); | |
| }); | |
| } | |
| const getLink = Key => { | |
| const minuteInSeconds = 60; | |
| const hourInSeconds = 60 * minuteInSeconds; | |
| const dayInSeconds = 24 * hourInSeconds; | |
| const weekInSeconds = 7 * dayInSeconds; | |
| const Expires = 1 * weekInSeconds; | |
| const params = { | |
| Key, | |
| Bucket, | |
| Expires | |
| } | |
| s3.getSignedUrl('getObject', params, function(err, data) { | |
| if (err) console.log('getLink error ->', err); | |
| else console.log('getLink data ->', data); | |
| }); | |
| } | |
| const keys = arguments.filePaths.split(','); | |
| keys.map(key => { | |
| console.log(`Secured AWS S3 link of ${key} is ${getLink(key)}`); | |
| }) | |
| // upload(); | |
| // putObject(); | |
| // getUploadLink(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
node s3-util.js filePaths='someName.png,someOtherFile.jpeg'command to upload files to S3 using the above file