I'm trying to upload PDF files using Multer-S3 directly to S3 using express server.
For some reason, it works when I try to upload SVG file it works great, but when I'm trying to upload something else like PDF there is no error, but when I try to access to file in my S3 bucket it's not opening or showing a blank PDF page.
My code:
filesService.js
const multer = require('multer');
const multerS3 = require('multer-s3-transform');
const AWS = require('aws-sdk');
const BUCKET_NAME = process.env.FILES_BUCKET_NAME;
const s3 = new AWS.S3({});
var limits = {
files: 1, // allow only 1 file per request
fileSize: 20 * 1024 * 1024 *1024, // (replace MBs allowed with your desires)
};
const upload = multer({
limits: limits,
storage: multerS3({
acl: "public-read",
s3: s3,
bucket: BUCKET_NAME,
contentType: multerS3.AUTO_CONTENT_TYPE,
key: (req, file, cb) => {
console.log(file);
cb(null, file.originalname)
}
})
}).single('file');
module.exports = {upload};
route.js
router.post('/files', async (req, res ,next) => {
await filesService.upload(req, res, (err) => {
if(err) {
console.error(err);
} else {
console.log(req.file);
location = req.file.location;
res.status(200).send({link: req.file.location});
}
});
res.status(500);
})
I'm trying to do it using Postman with the following configurations:
Headers: Content-Type - multipart/form-data
Body: key: file, value: PDF selected
There's no error returned from my route, but again, when I try to access a file different than SVG it doesnt work.