Search code examples
amazon-s3multermulter-s3

I am getting multer error : unexpected field when i was trying to upload files in aws s3 bucket


I am done with all setup of the s3 bucket and using the correct secret keys,

I want to upload a file from my application using nodejs APIs for uploading and get the file URL from the s3 bucket

here is my code:

require("dotenv").config()

const express = require('express')

const app = express();

app.listen(3001,()=>{
   console.log("Runnning.......")
});

const aws = require('aws-sdk')
const multer = require('multer')
const multerS3 = require('multer-s3');


aws.config.update({
  secretAccessKey: process.env.ACCESS_SECRET,
  accessKeyId: process.env.ACCESS_KEY,
  region: process.env.REGION,
});
const BUCKET = process.env.BUCKET
const s3 = new aws.S3();

const upload = multer({
  storage: multerS3({
    s3: s3,
    acl: "public-read",
    bucket: BUCKET,
    key: function (req, file, cb) {
        console.log(file);
        cb(null, file.originalname)
    }
  })
})

app.post('/upload', upload.single('file'), async function (req, res, next) {

  res.send('Successfully uploaded ' + req.file.location + ' location!')

})

Solution

  • For Future reference:

    • Multer S3 2.x is compatible with AWS SDK 2.x
    • Multer S3 3.x is compatible with AWS SDK 3.x

    Either upgrade AWS SDK, or downgrade Multer S3 (npm install [email protected]).

    Source