我对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基于下面的johns评论,我认为这是某种标头问题,因此我在客户端的我的put请求中添加了'x-amz-acl': 'public-read'
标头,但仍然给我同样的无效签名问题
答案 0 :(得分:0)
我在使用“ AmazonS3FullAccess”的IAM用户收到相同的错误。对我有用的是添加此CORS配置
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"PUT",
"POST"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": []
}
]