MulterS3 给出 this.client.send is not a function 错误

问题描述 投票:0回答:6

注: 在 multer s3 中出现以下错误。这个错误指向 NPM 模块,我真的不明白这个模块的问题。我已经使用 upload.single 和 upload.array 方法来检查这个模块的工作。但不工作。

代码:

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');
    });
  });
})();

错误:

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)

使用 multer-s3 寻找解决方案。提前谢谢你。

node.js rest multer multer-s3
6个回答
3
投票

你检查过这个问题了吗? https://github.com/anacronw/multer-s3/issues/169

请检查您的aws-sdk,multer-s3版本是否兼容


2
投票

只需按回车键:

npm i [email protected]

1
投票

3.x.x 版本的 multer-s3 使用 AWS JavaScript SDK v3。具体来说,它使用来自 @aws-sdk/lib-storage 的上传类,后者又调用模块化 S3Client。 查看他们的 github 自述文件

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

0
投票

我有同样的问题。将 Multer.s3 降级到 2x 版本解决问题!

Multer S3 2.x 兼容 AWS SDK 2.x 和 Multer S3 3.x 兼容 AWS SDK 3.x.


0
投票

对我来说,删除这条线是有效的

acl: 'public-read',

0
投票

加上之前的答案,我有同样的错误消息,我发现它是库兼容性问题。基本上:

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

值得检查 package.json 并查看您使用的是哪个版本。 所以要么升级 aws-sdk,要么降级 multer-s3。 在我的例子中,我将 multer-s3 降级了

npm i [email protected]
并且它有效

还值得在 github 上查看此问题以获取更多详细信息 https://github.com/anacronw/multer-s3/issues/169

© www.soinside.com 2019 - 2024. All rights reserved.