我正在尝试使用react-native
在react-native-aws3
中上传图像。我正在按照模块的docs和this来配置aws-iam
和aws-s3
。但是,当尝试上传图像时,我得到403
作为响应代码。我关注了许多博客和官方docs,但仍然无法找出错误。
index.js
...
handlePost = () => {
const options = {
keyPrefix: Config.AWS_S3_FOLDER,
bucket: Config.AWS_S3_BUCKET,
region: Config.AWS_REGION,
accessKey: Config.AWS_ACCESS_KEY,
secretKey: Config.AWS_SECRET_KEY,
successActionStatus: 201
}
console.log("options are ", options)
this.state.media.map(
image =>{
console.log("image is ", image, " and file name is ", image.replace(/^.*[\\\/]/, ''))
RNS3.put({uri:image, name:image.replace(/^.*[\\\/]/, ''), type: "image/jpeg"}, options)
.then((response)=> (console.log("response is ", response)))
}
)
};
...
环境变量是使用react-native-config
处理的,它们的加载方式如下:-
accessKey: "**"
bucket: "citra-iiits"
keyPrefix: "images/"
region: "ap-south-1"
secretKey: "**"
响应标头是:-
headers:
Connection: "close"
Content-Type: "application/xml"
Date: "Thu, 05 Dec 2019 12:49:38 GMT"
Server: "AmazonS3"
我的aws-iam
内联策略是:-
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1458840156000",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectAcl",
"s3:GetObjectVersion",
"s3:PutObject",
"s3:PutObjectAcl",
"s3:PutObjectVersionAcl"
],
"Resource": [
"arn:aws:s3:::citra-iiits/*"
]
}
]
}
桶策略是:-
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddCannedAcl",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<user>"
},
"Action": [
"s3:GetObject",
"s3:GetObjectAcl",
"s3:GetObjectVersion",
"s3:PutObject",
"s3:PutObjectAcl",
"s3:PutObjectVersionAcl"
],
"Resource": "arn:aws:s3:::citra-iiits/*"
}
]
}
Bucket cors政策是:-
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>