save image to s3 bucket with image formate

171 Views Asked by At

I am trying to upload an image to a s3 bucket using multerS3. It does save the file to the s3 bucket but not in an image format. here is my code.

  storage: multerS3({
    s3: S3,
    bucket: 'slsupload',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString()+".jpg")
    }
  })
});

const singleUpload = upload.single('file');

app.post('/test-upload', (req, res) => {
  singleUpload(req, res, function(err, some) {
    if (err) {
      return res.status(422).send({errors: [{title: 'Image Upload Error', detail: err.message}] });
    }

    return res.json({'imageUrl': req.file.location});
  });
});```
1

There are 1 best solutions below

0
On

You can try setting the contentType buy hand or let multerS3 discover the contentType automatically:

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})