简体   繁体   English

即使在添加 CORS 配置后 S3 的 CORS 错误

[英]CORS error with S3 even after adding CORS configuration

I am trying to upload images to an S3 bucket via javaScript.我正在尝试通过 javaScript 将图像上传到 S3 存储桶。 When submitting the request, the following error returns:提交请求时,返回以下错误:

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.

My CORS configuration on the bucket:我在存储桶上的 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>

I even manually added the origin to the policy as you can see, along with * , but nothing seems to be working, and I'm not sure where to begin.如您所见,我什至手动将原点添加到策略中,以及* ,但似乎没有任何效果,而且我不确定从哪里开始。

I examined the network pane in the Chrome Devtools and noticed that one of the XHR was apicture.png , and it had the following key information:我检查了 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

The response tab had some other important information: 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>

So, it is clear that they want the region to be us-west-1 however, if I change my AWS.config to that, throws a different error.因此,很明显,他们希望该区域为us-west-1但是,如果我将AWS.config更改为该区域,则会引发不同的错误。 Amazon's own guides say that the area "Northern California" are encoded as us-west-2 , so I am not sure why there is this inconsistency.亚马逊自己的指南说“北加州”区域被编码为us-west-2 ,所以我不确定为什么会出现这种不一致。

AWS S3 存储桶 => 添加元数据“缓存控制”:“无缓存”

Now you need to set in JSON.现在您需要在 JSON 中进行设置。 Permissions -> CORS权限 -> CORS

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM