gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:21:40 24 4
gpt4 key购买 nike

我正在尝试通过 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 中的网络 Pane ,发现其中一个 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 ,所以我不确定为什么会出现这种不一致。

最佳答案

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

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

关于javascript - 即使在添加 CORS 配置后,S3 也会出现 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41111244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com