【问题标题】:Configuring CORS in S3 Bucket Has No Effect在 S3 存储桶中配置 CORS 无效
【发布时间】:2022-04-01 10:06:42
【问题描述】:

我正在尝试在我的 S3 存储桶上配置 CORS 策略,以允许它与 snap.berkeley.edu 正确通信。但是,我在部署到 S3 后收到以下错误,在使用 localhost 测试时没有收到:

Access to XMLHttpRequest at 'https://cloud.snap.berkeley.edu/api/v1/init' from origin 'https://soundscope-website-beta.s3.amazonaws.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我已将其更改为以下 xml。但是,这样做并没有改变错误消息。

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>https://snap.berkeley.edu</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
<CORSRule>
    <AllowedOrigin>https://cloud.snap.berkeley.edu</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

我是否正确配置了 CORS 策略?这是 snap.berkeley.edu 的问题,而不是我在 AWS 上的问题吗?任何帮助/建议将不胜感激。

编辑:尝试使用代理后,我在本地主机上收到以下错误:

Access to XMLHttpRequest at 'https://cors-anywhere.herokuapp.com/https://cloud.snap.berkeley.edu/api/v1/init' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

POST https://cors-anywhere.herokuapp.com/https://cloud.snap.berkeley.edu/api/v1/init net::ERR_FAILED

以及来自 s3 存储桶的以下错误:

Access to XMLHttpRequest at 'https://cors-anywhere.herokuapp.com/https://cloud.snap.berkeley.edu/api/v1/init' from origin 'https://soundscope-website-beta.s3.amazonaws.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

【问题讨论】:

  • 这能回答你的问题吗? S3 - Access-Control-Allow-Origin Header
  • 问题中引用的错误消息表明问题是浏览器阻止了在https://soundscope-website-beta.s3.amazonaws.com(我猜是您的S3存储桶)上运行的前端JavaScript代码向@发出跨域请求987654327@。但您似乎一定忽略了实际的请求 URL,因为对“/api/v1/init”的请求实际上并不是跨域请求。 “/api/v1/init”的实际完整 URL 是什么?
  • 将您的 S3 存储桶配置为 https://snap.berkeley.eduhttps://cloud.snap.berkeley.edu 作为允许的来源不会让您的 S3 存储桶向这些来源发出请求。相反,它是相反的:它允许在 https://snap.berkeley.eduhttps://cloud.snap.berkeley.edu 上运行的前端 JavaScript 代码向 S3 存储桶的端点发出请求。这就是你真正想要启用的吗?或者您是否希望启用从您的 S3 存储桶运行的前端 JavaScript 代码以向 https://snap.berkeley.eduhttps://cloud.snap.berkeley.edu 端点发出请求?
  • @sideshowbarker 由于某种原因,我之前没有复制完整的错误消息;对于那个很抱歉。现在更新了。
  • @sideshowbarker 这是有道理的。我正在尝试后者 - 允许我的前端 JS 代码在那里发出请求。它在 localhost 上完美运行,这让我相信这是我在 AWS 上的 CORS 配置的问题。

标签: amazon-web-services amazon-s3 cors


【解决方案1】:

我遇到了同样的问题,很遗憾,讨论中的建议对我的情况没有帮助,所以我想我在这里添加我的解决方案,以防其他人遇到这个非常令人沮丧的问题。

在 S3 的文件属性选项卡中编辑元数据后,我终于让它工作了。添加“系统定义”类型,key:Cache-Control,value:no-store。

【讨论】:

    猜你喜欢
    • 2017-09-21
    • 2021-12-28
    • 2015-01-08
    • 2020-07-16
    • 2017-01-30
    • 2021-03-31
    • 2016-03-02
    • 2021-03-15
    • 2017-09-05
    相关资源
    最近更新 更多