【问题标题】:403 on `PUT` request on signed url for S3 image upload using lamda403 对使用 lambda 进行 S3 图像上传的签名 URL 的“PUT”请求
【发布时间】:2020-12-16 19:04:16
【问题描述】:

我正在尝试使用 Lambda 为我的 S3 存储桶创建一个图像上传器。但是当我尝试对从 lambda 生成的签名 URL 执行 PUT 请求时,我得到了 403。

我已将我的 lambda 配置为能够 s3:putObject 和 s3:putObjectACL 我还尝试将我的 s3 存储桶公开以允许对 Everyone 进行读写,并且还在我的 s3 存储桶上启用了 CORS 以及我的 API lambda 触发器端点。

当我尝试从客户端 HTML 页面上传图像时,我仍然收到 403 禁止。

函数是这样的:

 const uploadS3 = async (uri) =>{
                    const file = new Image();
                    file.src = uri;

                    const url = "API-Endpoint to trigger LAMBDA";
                    fetch(url,
                    {
                     //fetches the signed url
                        method: 'GET',
                        mode:'cors',
                    })
                    .then((res) => res.json())
                    .then((response)=> {
                      console.log(response);
                      console.log(response.uploadURL);

                    // request to try to PUT image to s3 bucket using the signed url
                      fetch(response.uploadURL,{
                        method: 'PUT',
                        headers: {
                          "Content-Type": "image/png"
                        },
                        mode:'cors',
                        body: file
                      })
                      .then((res)=> console.log(res))
                      .catch((res)=> console.log(res))
                    })
                    .catch((err)=>console.log(err))
                  }

我哪里错了? 非常感谢您的帮助!!

【问题讨论】:

    标签: javascript rest amazon-s3 file-upload aws-lambda


    【解决方案1】:

    在服务器端获取预签名 URL 以及在客户端使用预签名 URL 发出请求时,您都需要以下标头

    { headers: { 'Content-Type': 'image/jpeg', 'x-amz-acl': 'public-read' } };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 2021-02-26
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 2020-11-15
      • 2022-07-16
      • 2018-07-06
      相关资源
      最近更新 更多