上传到S3存储桶时出现403禁止错误

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

我对 AWS 还很陌生,但我相当确定我的 IAM 用户设置正确...除了

AmazonS3FullAccess
之外,我还需要添加其他权限吗?这个名字意味着它应该足够了......要么是权限问题,要么是我的代码搞砸了。

我试图按照 https://devcenter.heroku.com/articles/s3-upload-node 上的指南进行操作。任何帮助,将不胜感激。 :)

这是我的相关代码:

//server side code
router.get('/sign-s3', (req, res) => {
  const s3 = new aws.S3();
  const { fileName, fileType } = req.query;
  s3.getSignedUrl('putObject', {
      Bucket: S3BUCKET,
      Key: fileName,
      Expires: 60,
      ContentType: fileType,
      ACL: 'public-read'
  }, (err, data) => {
      if (err) {
        console.log(err);
        res.status(500).json(err)
      }
      res.json({
        signedRequest: data,
        url: `https://${S3BUCKET}.s3.amazonaws.com/${fileName}`
    });
  });
});

//client side code
const onChangeHandler = (e) => {
    const file = e.target.files[0];
    axios
        .get(`/api/bucket/sign-s3?fileName=${file.name}&fileType=${file.type}`)
        .then(signedResponse => {
            axios
            .put(signedResponse.data.signedRequest,file, {    
                headers: {
                'Content-Type': 'multipart/form-data'
              }
            })
            .then(response => {
                console.log("upload successful");
                props.addImages([signedResponse.data.url]);
            })
            .catch(error => console.error(error));
        })
        .catch(error => console.error(error));
}

以及我的错误的屏幕截图:

更新: 从我的标志路线中删除

ACL: 'public-read'
线可以让上传通过,但没有人可以访问图像。 :P 基于下面的约翰评论,我认为这是某种标头问题,所以我在客户端的放置请求中添加了
'x-amz-acl': 'public-read'
标头,但它仍然给我带来了相同的无效签名问题

amazon-web-services rest amazon-s3 http-status-code-403
2个回答
0
投票

我在使用“AmazonS3FullAccess”的 IAM 用户时收到相同的错误。对我有用的是添加这个 CORS 配置

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "PUT",
            "POST"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]

0
投票

就我而言,我已经更新了访问密钥,但在创建 S3 实例时使用旧的密钥

 s3 = new AWS.S3({
    credentials: {
      accessKeyId: config.accessKeyId,          // KEEP UPDATED ACCESS KEY   
      secretAccessKey: config.secretAccessKey,  // KEEP UPDATED SECRET ACCESS KEY
    },```
© www.soinside.com 2019 - 2024. All rights reserved.