sau*_*kko 7 javascript amazon-s3 cors
我在该eu-west-1地区有一个 S3 存储桶,我们称之为my-bucket.
在那个桶里有一张照片,我们称之为some-file.jpg。
如果我通过浏览器访问这两个 url,我可以检索该图片(存储桶中的所有对象都是公开的)(请记住,这些是示例,而不是现实生活中的 url):
https://my-bucket.s3.amazonaws.com/some-file.jpg
https://s3-eu-west-1.amazonaws.com/my-bucket/some-file.jpg
但是,我正在尝试获取有关该图片的一些信息,为此我使用了 vibrity.js,它尝试检索该文件。
但是,当 url 为https://my-bucket.s3.amazonaws.com/some-file.jpg时它会失败,并出现以下 CORS 错误:
Access to Image at 'https://my-bucket.s3.amazonaws.com/some-file.jpg' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://example.com' 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>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
Run Code Online (Sandbox Code Playgroud)
我的猜测是https://s3-eu-west-1.amazonaws.com/my-bucket/some-file.jpg发送Access-Control-Allow-Origin而另一个没有。我该如何解决这个问题?
Ton*_*nio 10
更新 CORS 标头后是否清除了浏览器缓存
我遇到的问题是Chrome 正在缓存带有标头(不包含 CORS 数据)的图像,所以无论我尝试在 AWS 中更改什么,我都不会看到我的 CORS 标头。
之后清除浏览器缓存和重新载入页面,图像具有预期的CORS头
小智 -4
尝试通过 CORS 代理运行图像。
fetch('https://cors.now.sh/https://my-bucket.s3.amazonaws.com/some-file.jpg')
.then(console.log)
.catch(console.error)
Run Code Online (Sandbox Code Playgroud)
这看起来像是一个黑客,但对我来说非常有效。