我将尝试通过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.
我的存储桶(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>
我甚至手动将源添加到策略中,如您所见,还有*
,但似乎没有任何作用,我不知道从哪里开始。
我查看了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
响应
选项卡还有其他重要信息:
<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
,所以我不确定为什么会存在这种不一致性。