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.
As an alternative, and since we chose to make our PDF much bigger, we took a decision to move the PDF generator from the front-end to the backend, so there was no need using Multer for that anymore, we generated the PDF on Express backend server using pdfkit
npm package, then uploaded it once it done generating the PDF directly from the same server to the related S3 bucket, using aws-sdk
npm package.
So, finally what we chose to do is just to send the related PDF data from the front to the backend and generate the PDF there.