0

I've created an api to upload image to amazon s3 bucket with nodejs, multer, and multer-s3 . Its working fine in development returning me a response of image URL which is downloadable and accessible but when I host my node app on Aws Lambda (API GATEWAY) the API returns me the same response with the image URL but this time when I open the downloaded image it shows me INVALID FILE FORMATTED

Here is my Code

    const uploadImage = async (req,res) => {
  let myFile = req.file.originalname.split(".")
    const fileType = myFile[myFile.length - 1]

    const params = {
        Bucket: 'test-bucket2601',
        Key: `${Date.now()}.${fileType}`,
        Body: req.file.buffer
    }

  s3.upload(params, (error, data) => {
        if(error){
            res.status(500).send(error)
        }
        res.json({data})
        
    })

    
}

Route of middleware

routes.route('/upload').post(upload,uploadImage);

in post method the first argument is upload middleware

Middleware Code:

    const s3 = new aws.S3({
    credentials: {
    accessKeyId: awsKeys?.accessKeyId,
    secretAccessKey: awsKeys?.secretAccessKey 
    }
   });

   const storage = multer.memoryStorage({
     destination: function(req, file, callback) {
      callback(null, '')
     }})

    const upload = multer({storage}).single('imageUrl')

1 Answers1

0

thank you for your posting. Please attach responseType property with "arraybuffer" to request headers. It will work. Thank you.

hotbrainy
  • 331
  • 1
  • 10