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.

Jay Bhajiyawala
  • 101
  • 1
  • 4
  • 11

6 Answers6

7

Just hit the enter:

npm i multer-s3@2.10.0
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Aditya Gupta
  • 71
  • 1
  • 2
5

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);
    },
  }),
});
3

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

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

dante
  • 933
  • 4
  • 16
  • 39
1

For me, removing this line worked

acl: 'public-read',
Kushagra Kumar
  • 131
  • 1
  • 3
0

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.

0

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 multer-s3@2.10.0 and it worked

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

Yusuf
  • 2,295
  • 7
  • 15
  • 34