Bucket Name Duplicated in AWS S3 File Location

291 Views Asked by At

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.

1

There are 1 best solutions below

0
Masood Ahmad On

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/[email protected]

Link to Original Issue at Github