我完全无法使用 multer-s3 将图像上传到 aws s3 存储桶

I'm totally faint in using multer-s3 to upload image to aws s3 bucket

下面代码中的"upload.array('photos', 3) "是什么意思....

我的 html 代码。 (为简洁起见减少了代码)

 <form action="/upload" method="POST">
 <input type="file" name="pic" id="pic" accept="image/*">
 <input type="submit">
 </form>

我的 expressjs 代码

AWS.config.update({
  accessKeyId: "xxxxxxxxxxxxx",
  secretAccessKey: "yyyyyyyyyy", 
  "region": "zzzzzzzzz" ,
  signatureVersion: 'v4'
});

var s3=new AWS.S3();

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'imgcontainer',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

app.post('/upload', upload.array('photos', 3), function(req, res, next) {
  res.send('Successfully uploaded ' + req.files.length + ' files!')
})

输出:

 Cannot read property 'length' of undefined

有两个问题:

  • 文件字段名是'pic'而不是'photos'(这是在Express端使用的字段名)。换个名字吧。

  • 需要在<form>标签上显式设置enctype="multipart/form-data"属性,否则浏览器将发送表单为application/x-www-form-urlencoded,不包括文件输入数据.

进行这些更改,req.files 应按照 multer 文档中的概述进行填充。