Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const storage = multer.memoryStorage({
- destination: function (req, file, cb) {
- cb(null, '')
- }
- })
- const filefilter = (req, file, cb) => {
- if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/jpg') {
- cb(null, true)
- } else {
- cb(null, false)
- }
- }
- // defining the upload variable for the configuration of photo being uploaded
- const upload = multer({ storage: storage, fileFilter: filefilter }).fields([{name:'image',maxCount:1}]);
- const s3 = new Aws.S3({
- accessKeyId:config.get('AWSAccessKeyId'), // accessKeyId that is stored in .env file
- secretAccessKey:config.get('AWSSecretKey'), // secretAccessKey is also store in .env file
- })
- router.post('/',upload, async (req, res, next) => {
- const params = {
- Bucket:"imgstrg", // bucket that we made earlier
- Key:req.file.originalname, // Name of the image
- Body:req.file.buffer, // Body which will contain the image in buffer format
- ACL:"public-read-write", // defining the permissions to get the public link
- ContentType:"image/jpeg" // Necessary to define the image content-type to view the photo in the browser with the link
- };
- s3.upload(params,(error,data)=>{
- if(error){
- res.status(404).send({"error":error});
- }
- res.send(data.Location);
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement