Every time I upload a file to my bucket from my website two copies get uploaded. Is there anything wrong with my code that makes this happen?
const multer = require('multer');
const multers3 = require('multer-s3');
const path = require('path'); // Add this for file extension handling
AWS_SDK_LOAD_CONFIG=1
aws.config.update({
accessKeyId: 'MyAccessKeyId',
secretAccessKey: 'MySecretKey',
});
const fileFilter = (req, file, cb) => {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb('Error: Images Only!');
}
};
const s3 = new aws.S3({});
module.exports = {
uploadImage: multer({
storage: multers3({
s3: s3,
bucket: 'my-bucket',
acl: 'public-read',
metadata: (req, file, cb) => {
cb(null, { fieldName: file.fieldname });
},
key: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, uniqueSuffix + '-' + file.originalname);
},
}),
limits: { fileSize: 3 * 1024 * 1024 }, // 3 MB
fileFilter: fileFilter,
})
};
I have tried changing a few things but nothing has worked, are multer and multer 3 both uploading the same file to my bucket?
Ok I found it was related to uniqueSuffix that was creating two different keys for when a file was uploaded to the bucket and when the same file was getting retrieved from the bucket. This created two different copies of the same file in my bucket. To overcome this and keep the files unique. I decided to use a key of originalfilename+userid+timestamp, excluding seconds to ensure the time between upload and retrival was the same and hence the key was the same for the file for both upload and retrieval. Here is the code if anyone is interested.