Advertisement
virtuoso_o

multer error

Jul 19th, 2022
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const storage = multer.memoryStorage({
  2.  
  3.     destination: function (req, file, cb) {
  4.         cb(null, '')
  5.     }
  6. })
  7.  
  8. const filefilter = (req, file, cb) => {
  9.     if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/jpg') {
  10.         cb(null, true)
  11.     } else {
  12.         cb(null, false)
  13.     }
  14. }
  15.  
  16. // defining the upload variable for the configuration of photo being uploaded
  17. const upload = multer({ storage: storage, fileFilter: filefilter }).fields([{name:'image',maxCount:1}]);
  18.  
  19. const s3 = new Aws.S3({
  20. accessKeyId:config.get('AWSAccessKeyId'),              // accessKeyId that is stored in .env file
  21. secretAccessKey:config.get('AWSSecretKey'),       // secretAccessKey is also store in .env file
  22. })
  23.  
  24. router.post('/',upload, async (req, res, next) => {
  25.  
  26.  
  27.     const params = {
  28.         Bucket:"imgstrg",      // bucket that we made earlier
  29.         Key:req.file.originalname,               // Name of the image
  30.         Body:req.file.buffer,                    // Body which will contain the image in buffer format
  31.         ACL:"public-read-write",                 // defining the permissions to get the public link
  32.         ContentType:"image/jpeg"                 // Necessary to define the image content-type to view the photo in the browser with the link
  33.     };
  34.  
  35.     s3.upload(params,(error,data)=>{
  36.         if(error){
  37.             res.status(404).send({"error":error});
  38.         }
  39.         res.send(data.Location);
  40.     })
  41. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement