使用预签名 URL 上传到 S3 私有存储桶时出现 403 错误

403 error on upload to S3 private bucket using pre-signed URL

我有一个使用 Amplify 的 React 应用程序,其中每个用户在 S3 存储桶中都有一个私人文件夹。用户只能将文件上传和下载到他们的私有存储桶。我希望某些用户能够使用 S3 预签名 URLs.

将文件上传到其他用户的私人文件夹

我在后端创建了一个 Lambda 函数来生成预签名 URL:

// do some authorization checks...
let key = uuidv4() + '.pdf'
let params = {
   Bucket: config.bucket,
   Key: key,
   ContentType: 'application/pdf',
   Expires: config.signedUrlExpirySeconds,
}
const url = await s3.getSignedUrl('putObject', params)
return url

我的前端现在有 URL,其中


const [ file, setFile ] = useState(null)

const handleChange = async (e) => {
   const { target: { value, files }} = e
   setFile(files[0])
}

const getURL = async () => {
   // get the pre-signed URL...
}

const upload = async (url, type) => {
  try {
    const result = await axios.put(url, file, {
      headers: {
        'Content-Type': type
      }
    })
  }
  catch (err) {
    console.log(err)
  }
}

const handleUpload = async () => {
   const url = await getURL()
   await upload(url, 'application/pdf')
}

return (
   <div>
      <input type="file" accept=".pdf" onChange={handleChange} />
      <button onClick={handleUpload}>Upload</button>
   </div>
)

但是,这个 returns 403 错误:

Error: Request failed with status code 403
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:62)

有什么我遗漏的吗?

可能是存储桶策略或 CORS,或者我是否使用 useState 来存储上传的文件(或者我是否需要将其转换为其他内容)?

如果有帮助,我的存储桶策略是:

{
    "Version": "2012-10-17",
    "Id": "Policy1624459513917",
    "Statement": [
        {
            "Sid": "Stmt1624459491997",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::##########/public/images/*"
        },
        {
            "Sid": "Stmt1624459491997b",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::##########/protected/*/images/*"
        }
    ]
}

根据 OP 的评论,将 s3:PutObject 添加到 lambda 角色有效。