将图像上传到Amazon S3返回400错误请求

时间:2019-01-28 06:35:19

标签: amazon-web-services amazon-s3 multer-s3

上传时,它总是返回400错误请求。我正在使用multer将图像上传到s3

这是我的存储桶策略

{
    "Version": "2012-10-17",
    "Id": "Policy1548648592430",
    "Statement": [
        {
            "Sid": "Stmt1548648588327",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::237240578789:user/megatron.578@gmail.com"
            },
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::test-test-bucket-test/*"
        }
    ]
}

马尔特:

const uploadImage = multer({
  storage: multerS3({
    s3, // compose of aws key, secret key and bucket name
    bucket: 'test-test-bucket-test',
    region: 'us-east-2',
    acl: 'public-read',
    key(req, file, cb) {
      cb(
        null,
        `images/${path.basename(
          file.originalname,
          path.extname(file.originalname),
        )}-${Date.now()}${path.extname(file.originalname)}`,
      );
    },
  }),
  limits: { fileSize: 2000000 },
  fileFilter(req, file, cb) {
    checkFileType(file, cb);
  },
}).array('images', 3);

路由器:

router.post('/uploadImages', (req, res) => {
  uploadImage(req, res, error => {
    if (error) {
      console.log(error);
      res.status(400).send({ Errors: error });
    } else {
      if (req.files === undefined) {
        res.status(400).send({ Errors: 'No File Selected' });
      }
      // If Success
      const { files } = req;
      let fileLocation = null;
      const locArray = [];
      for (let i = 0; i < files.length; i += 1) {
        fileLocation = files[i].location;
        locArray.push(fileLocation);
      }
      // Save the file name into database
      res.send({
        success: true,
        files,
      });
    }
  });

});

我也只是遵循有关上传的教程。它总是返回状态为“ 400”的“错误请求”,并且无法查看其表示拒绝权限的响应。

0 个答案:

没有答案