即使添加 CORS 配置后,S3 也会出现 CORS 错误

Mar*_*her 5 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.
Run Code Online (Sandbox Code Playgroud)

我在存储桶上的 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>
Run Code Online (Sandbox Code Playgroud)

我什至手动将原点添加到策略中,如您所见*,但似乎没有任何效果,我不知道从哪里开始。

我检查了 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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

因此,很明显他们希望该区域是us-west-1这样的,但是,如果我将其更改AWS.config为该区域,则会引发不同的错误。亚马逊自己的指南说“北加州”区域被编码为us-west-2,所以我不确定为什么会出现这种不一致。

小智 0

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