我有一个使用 Amplify 的 React 应用程序,其中每个用户在 S3 存储桶中都有一个私人文件夹。用户只能将文件上传和下载到自己的私有存储桶中。我希望某些用户能够使用 S3 预签名 URL 将文件上传到其他用户的私人文件夹。
我在后端创建了一个 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>
)
但是,这会返回 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/*"
}
]
}