S3:请求的资源上不存在访问控制允许源标头



我在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

但是,我

正在尝试获取有关该图片的一些信息,为此我正在使用vibrant.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

我已确保存储桶的 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>

我的猜测是 https://s3-eu-west-1.amazonaws.com/my-bucket/some-file.jpg 发送Access-Control-Allow-Origin而另一个没有。我该如何解决这个问题?

更新 CORS 标头后是否清除了浏览器缓存

我遇到的问题是 Chrome 使用标头(不包含 CORS 数据(缓存图像,因此无论我尝试在 AWS 中更改什么,我都不会看到我的 CORS 标头。

清除 Chrome 缓存并重新加载页面后,图像具有预期的 CORS 标头

尝试通过 CORS 代理运行映像。

fetch('https://cors.now.sh/https://my-bucket.s3.amazonaws.com/some-file.jpg')
  .then(console.log)
  .catch(console.error)

这看起来像一个黑客,但对我来说非常有效。

相关内容

最新更新