添加CORS配置后,S3的CORS错误

时间:2016-12-12 23:13:08

标签: javascript amazon-s3 cors

我正在尝试通过javaScript将图像上传到S3存储桶。提交请求时,将返回以下错误:

XMLHttpRequest cannot load https://somebucket.s3-us-west 
2.amazonaws.com/albums//apicture.png. Response to preflight 
request doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource. 
Origin 'http://someorigin:3000' is therefore not allowed access.

我在桶上的CORS配置:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://someorigin:3000</AllowedOrigin>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

我甚至手动将原点添加到政策中,您可以看到*,但似乎没有任何效果,我不知道从哪里开始。

我检查了Chrome Devtools中的网络窗格,发现其中一个XHR是apicture.png,它有以下关键信息:

Request URL:https://somebucket.s3-us-west-2.amazonaws.com/pics/apicture.png
Request Method:OPTIONS
Status Code:301 Moved Permanently

response标签包含其他一些重要信息:

<Error>
<Code>PermanentRedirect</Code>
<Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message>
<Bucket>somebucket</Bucket>
<Endpoint>somebucket.s3-us-west-1.amazonaws.com</Endpoint>
<RequestId>
***************
</RequestId>
</Error>

因此,显然他们希望该区域为us-west-1但是,如果我将AWS.config更改为该区域,则会抛出不同的错误。亚马逊自己的导游说,该地区&#34;北加州&#34;被编码为us-west-2,所以我不确定为什么会出现这种不一致。

2 个答案:

答案 0 :(得分:0)

AWS S3存储桶=>添加元数据'Cache-Control':'no-cache'

答案 1 :(得分:0)

现在您需要在 JSON 中进行设置。 权限 -> CORS

[{
    "AllowedHeaders": [
        "*"
    ],
    "AllowedMethods": [
        "GET"
    ],
    "AllowedOrigins": [
        "*"
    ],
    "ExposeHeaders": []
}]