I am trying to use Multer-S3 to upload a JSON file to S3. I can get it to upload if I set the filename on the backend to something like Date.now().toString() however when I try to set it to the name I want and pass from my frontend it fails. My goal is to be able to set the filename in a way that allows me to have S3 prefixes making it easier to process data later
Frontend
const userID = this.$store.getters.userID
const accountCode = userID.account_code
let formData = new FormData()
if(this.upload){
formData.append('import', this.upload)
formData.append("fileName", `${accountCode}/${Date.now()}`)
axios.post('/management/import', formData)
.then(() => {
this.successMessage = 'Successfully uploaded file'
})
.catch(error => {
console.error(error)
this.errorMessage = 'Error uploading file please try again'
})
}
Backend
const upload = multer({
storage: multerS3({
s3: s3,
bucket: config.AWS.IMPORT_BUCKET_NAME,
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
cb(null, Date.now().toString()) // Want this to be the formData.fileName seen in the frontend code
}
})
})
router.post('/import', upload.single('import'), async(req, res) => {
console.log(req.file)
return res.json({success:true})
}