Skip to content

Instantly share code, notes, and snippets.

@victorpaulo
Created August 26, 2019 18:29
Show Gist options
  • Save victorpaulo/b89c62e1e348ef5193886505ac082bc8 to your computer and use it in GitHub Desktop.
Save victorpaulo/b89c62e1e348ef5193886505ac082bc8 to your computer and use it in GitHub Desktop.
AWS S3 library
const AWS = require('aws-sdk');
require('dotenv').config();
module.exports = {
uploadToS3: function (file) {
let s3bucket = new AWS.S3({
accessKeyId: process.env.IAM_USER_KEY,
secretAccessKey: process.env.IAM_USER_SECRET,
Bucket: process.env.BUCKET_NAME,
});
return new Promise(function(resolve, reject) {
s3bucket.createBucket(function () {
var params = {
Bucket: process.env.BUCKET_NAME,
Key: file.name,
Body: file.data,
ContentType: file.mimetype,
ACL:'public-read'
};
s3bucket.upload(params, function (err, data) {
if (err) {
console.log('error in callback');
reject(err);
}
console.log('success');
resolve(data);
});
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment