1

Im uploading image files to my S3 bucket with multer S3. Everthing goes smooth, file does get uploaded but bucket name gets duplicated in returned FILE LOCATION:

Here's the file location is s3: CORRECT in S3 https://MYBUCKET.s3.eu-west-3.amazonaws.com/1669200254736-img-intro-bg.jpg

Here's what I get as file.location in my node app: Bucket name is doubled: https://MYBUCKET.MYBUCKET.s3.eu-west-3.amazonaws.com/1669200254736-img-intro-bg.jpg'

HERE's MY APP CODE;


const { S3Client } = require('@aws-sdk/client-s3');
const multer = require('multer');
const multerS3 = require('multer-s3');
require('dotenv').config();


const credentials = {
  region: process.env.region,
  credentials: {
    accessKeyId: process.env.accessKeyId,
    secretAccessKey: process.env.secretAccessKey,
  },
};

const s3 = new S3Client(credentials);

const imageFilter = (req, file, cb) => {
  if (file.mimetype.startsWith('image')) {
    cb(null, true);
  } else {
    cb('Please upload only images.', false);
  }
};

const uploadFile = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'MYBUCKET',
    metadata: function (req, file, cb) {
      cb(null, { fieldName: file.fieldname });
    },
    key: function (req, file, cb) {
      cb(null, `${Date.now()}-img-${file.originalname}`);
    },
  }),
  fileFilter: imageFilter,
  limits: {
    fieldSize: '50mb',
  },
});

module.exports = uploadFile;


Thank you in advance for your support.

I've been searching online for a few days but of no avail.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

1

Apparently the problem stems form underlying Amazon S3 sdk. The most recent s3 client sdk still has the issue.

Downgrade Amazon sdk to < 3.180.0, untill issue is fixed by AWS.

npm i @aws-sdk/client-s3@3.180.0

Link to Original Issue at Github