Last active
April 18, 2021 09:34
-
-
Save otoloye/57c2374abda8258e77dd049c1ff1db97 to your computer and use it in GitHub Desktop.
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
import { userModel as Users } from '../models'; | |
import aws from 'aws-sdk'; | |
import fs from 'fs'; | |
export default { | |
signup(req, res) { | |
aws.config.setPromisesDependency(); | |
aws.config.update({ | |
accessKeyId: process.env.ACCESSKEYID, | |
secretAccessKey: process.env.SECRETACCESSKEY, | |
region: process.env.REGION | |
}); | |
const s3 = new aws.S3(); | |
var params = { | |
ACL: 'public-read', | |
Bucket: process.env.BUCKET_NAME, | |
Body: fs.createReadStream(req.file.path), | |
Key: `userAvatar/${req.file.originalname}` | |
}; | |
s3.upload(params, (err, data) => { | |
if (err) { | |
console.log('Error occured while trying to upload to S3 bucket', err); | |
} | |
if (data) { | |
fs.unlinkSync(req.file.path); // Empty temp folder | |
const locationUrl = data.Location; | |
let newUser = new Users({ ...req.body, avatar: locationUrl }); | |
newUser | |
.save() | |
.then(user => { | |
res.json({ message: 'User created successfully', user }); | |
}) | |
.catch(err => { | |
console.log('Error occured while trying to save to DB'); | |
}); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment