MulterS3 is giving this.client.send is not a function error
Asked Answered
P

6

5

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.

Pickaxe answered 30/5, 2022 at 9:3 Comment(0)
N
14

Just hit the enter:

npm i [email protected]
Nunnery answered 3/11, 2022 at 18:24 Comment(2)
how does this solve the problem?Colly
because this version is compatible with AWS SDKSelenite
B
6

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);
    },
  }),
});
Beetner answered 2/8, 2022 at 17:27 Comment(0)
A
3

Have you checked this issues ? https://github.com/anacronw/multer-s3/issues/169

please check your aws-sdk, multer-s3 version is compatible

Alanson answered 1/6, 2022 at 12:13 Comment(0)
S
3

For me, removing this line worked

acl: 'public-read',
Strudel answered 24/1, 2023 at 5:45 Comment(0)
C
2

Adding on the previous answers, I have had the same error msg and I found out that it's libraries compatibility issue. Basically:

multer-S3 v 2.x is compatible with aws-sdk v2.x 
multer-S3 v 3.x is compatible with aws-sdk v3.x 

its worth checking package.json and see what version of both you are using. so either upgrade aws-sdk, or downgraded multer-s3. in my case I downgrade multer-s3 by npm i [email protected] and it worked

also it worth checking this issue on github for more details https://github.com/anacronw/multer-s3/issues/169

Citronella answered 23/3, 2023 at 1:1 Comment(0)
M
1

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.

Merino answered 3/6, 2022 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.