我正在使用 S3 从 UI 上传图像。我有一个带有访问 key ID/ secret 访问 key 的 AMI,仅允许 putObject
和 getObject
。所有 putObject
均通过 public-read
acl 调用。
我在 S3 存储桶中有此 CORS 配置:
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>https://foo.com</AllowedOrigin>
<AllowedOrigin>http://localhost:5000</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
</CORSRule>
</CORSConfiguration>
我不断收到这些 CORS 错误:
XMLHttpRequest cannot load https://foo.s3.amazonaws.com/items/images/e75768-2018-0-26/roar-bomber.png. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://foo.com' is therefore not allowed access. The response had HTTP status code 403.
我显然允许正确的起源,所以我不知道为什么它一直对我大喊大叫。但更糟糕的是,它有时会起作用,然后即使没有任何改变,也会再次停止工作......
使用 Chrome 浏览器。
最佳答案
更新
AWS 不允许将 OPTIONS
方法设置为允许的方法。
遵循指南https://docs.aws.amazon.com/en_pv/AmazonS3/latest/dev/cors.html#how-do-i-enable-cors ,检查AllowedMethod Element部分。
添加 CORS 配置中允许的 OPTIONS
方法。
预检请求使用 OPTION 方法来检查 CORS 允许使用哪些方法。
https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
关于javascript - S3 CORS 错误永远不会结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48487756/