无法使用react-native-aws3上传图片

时间:2019-12-05 13:00:58

标签: react-native amazon-s3

我正在尝试使用react-nativereact-native-aws3中上传图像。我正在按照模块的docsthis来配置aws-iamaws-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>

0 个答案:

没有答案