Created
August 25, 2019 18:03
-
-
Save Ifmr24/70da469461010af0782ef7826bb365df to your computer and use it in GitHub Desktop.
S3 Upload Files
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
require('dotenv').config() | |
var aws = require('aws-sdk'); | |
aws.config.update({ | |
region: 'us-east-1', | |
accessKeyId: process.env.AWSAccessKeyId, | |
secretAccessKey: process.env.AWSSecretKey | |
}) | |
const S3_BUCKET = process.env.bucket | |
router.post("/api/uploadavatar", async (req, res) => { | |
const s3 = new aws.S3(); | |
const fileName = req.body.fileName; | |
const fileType = req.body.fileType; | |
const folder = req.body.folder; | |
const s3Params = { | |
Bucket: S3_BUCKET + '/' + folder, | |
Key: fileName, | |
Expires: 500, | |
ContentType: fileType, | |
ACL: 'public-read' | |
}; | |
s3.getSignedUrl('putObject', s3Params, (err, data) => { | |
if (err) { | |
console.log(err); | |
res.json({ success: false, error: err }) | |
} | |
const returnData = { | |
signedRequest: data, | |
url: `https://${S3_BUCKET}.s3.amazonaws.com/${folder}/${fileName}` | |
}; | |
res.json({ success: true, data: { returnData } }); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment