【发布时间】:2017-04-27 22:22:32
【问题描述】:
我正在尝试通过 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 更改为该区域,则会引发不同的错误。亚马逊自己的指南说“北加州”区域编码为us-west-2,所以我不确定为什么会出现这种不一致。
【问题讨论】:
-
Response to preflight- 看起来是预检失败 - 您使用什么 http 方法(GET、POST 等)以及什么请求标头 - 检查开发人员工具网络选项卡以查看网络流量详细信息您的要求 -
@JaromandaX POST,在“请求标头”下有很多。你有什么特别想的吗?
-
不,只是非标准的确实会触发预检 - 根据消息,这显然是故障点 - 不熟悉 amazon-s3 所以我真的帮不上忙,除了指出它是失败的预检(即 OPTIONS 调用)
-
存储桶实际在哪个区域? N. California 是 us-west-1,而不是 us-west-2(即俄勒冈州)。
-
存储桶位于加利福尼亚北部。如果我将代码更改为 us-west-1,它会显示“OPTIONS cognito-identity.us-west-1.amazonaws.com net::ERR_NAME_NOT_RESOLVED”,可能是因为身份池 ID 在 us-west-2 中,但我无法更改,所以我真的迷路了。
标签: javascript amazon-s3 cors