MulterS3 is giving this.client.send is not a function error

Viewed 3138

Note: Getting below error in multer s3. This error is pointing NPM module and I really don't understand the issue with this module. I have used upload.single as well as upload.array method to check the working of this module. But not working.

Code:

const bodyParser = require('body-parser');
const cors = require('cors');

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

const http = require('http');
const app = express();

app.set('port', 3000);
app.use(
  bodyParser.json({
    limit: '50mb'
  })
);
app.use(
  bodyParser.urlencoded({
    limit: '50mb',
    extended: true
  })
);
app.use(cors());

aws.config.update({
  accessKeyId: '',
  secretAccessKey: ''
});

s3 = new aws.S3();

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: '',
    acl: 'public-read',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    metadata: function (req, file, cb) {
      cb(null, {
        fieldName: file.fieldname
      });
    },
    key: function (req, file, cb) {
      let extArray = file.mimetype.split('/');
      let ext = extArray[extArray.length - 1];

      console.log(`ext ->> `, ext, ` file.fieldname ->> `, file.fieldname);

      cb(null, "test/" + Date.now().toString() + '.' + ext);
    },
    /* limits: {
      fileSize: 1024 * 1024 * 10
    } */
  })
});

(() => {
  server = http.createServer(app).listen(app.get('port'), () => {
    console.debug(`Server started ->> `);
    app.get('/test', (req, res) => {Mul
      res.send('Hello');
    });

    app.post('/media', upload.single('media'), (req, res) => {
      console.log(`req.files ->> `, req.file);
      res.send('Thanks for you waiting time');
    });
  });
})();

Error:

ext ->>  jpeg  file.fieldname ->>  media
TypeError: this.client.send is not a function
    at Upload.__uploadUsingPut (/home/tristate/Jay/Force/node_modules/@aws-sdk/lib-storage/dist-cjs/Upload.js:52:25)
    at Upload.__doConcurrentUpload (/home/tristate/Jay/Force/node_modules/@aws-sdk/lib-storage/dist-cjs/Upload.js:99:39)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Looking solution with multer-s3. Thank you in advance.

3 Answers

I had this same issue. Downgrading Multer.s3 to 2x version solve the problem!

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

3.x.x releases of multer-s3 use AWS JavaScript SDK v3. Specifically, it uses the Upload class from @aws-sdk/lib-storage which in turn calls the modular S3Client. check their github readme

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

let s3 = new S3Client({
  region: 'ca-central-1',
  credentials: {
    accessKeyId: process.env.ACCESS_KEY_AWS,
    secretAccessKey: process.env.ACCESS_SECRET_AWS,
  },
  sslEnabled: false,
  s3ForcePathStyle: true,
  signatureVersion: 'v4',
});

exports.upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: process.env.BUCKET_AWS,
    contentType: multerS3.AUTO_CONTENT_TYPE,
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, { fieldName: file.fieldname });
    },
    key: function (req, file, cb) {
      cb(null, shortId.generate() + '-' + file.originalname);
    },
  }),
});
Related