Created
August 26, 2019 18:29
-
-
Save victorpaulo/b89c62e1e348ef5193886505ac082bc8 to your computer and use it in GitHub Desktop.
AWS S3 library
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'); | |
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