有人设法在响应头中添加Access-Control-Allow-Origin吗? 我需要的是这样的东西:

<img src="http://360assets.s3.amazonaws.com/tours/8b16734d-336c-48c7-95c4-3a93fa023a57/1_AU_COM_180212_Areitbahn_Hahnkoplift_Bergstation.tiles/l2_f_0101.jpg" />

这个get请求应该在响应中包含Access-Control-Allow-Origin: *

我的CORS设置桶看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

正如您所期望的那样,没有Origin响应头。


当前回答

就像其他人说的,你首先需要在你的S3桶中有CORS配置:

<CORSConfiguration>
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod> <!-- Add this -->
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>

但在我做了这些之后,它仍然不起作用。我用的是Chrome(可能其他浏览器也有同样的问题)。

问题是Chrome是缓存图像的头(不包含CORS数据),所以无论我试图改变AWS,我不会看到我的CORS头。

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

其他回答

fwuensche“答案”是正确的设置CDN;这样做,我删除了maxagesecseconds。

<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>

见以上答案。(但我也有一个chrome bug)

不要在CHROME中加载和显示图片。(如果稍后要创建一个新实例)

在我的例子中,我加载图像并在页面上显示它们。当它们被点击时,我创建了它们的一个新实例:

// there is already an html <img /> on the page, I'm creating a new one now
img = $('<img crossorigin />')[0]
img.onload = function(){
  context.drawImage(img, 0, 0)
  context.getImageData(0,0,w,h)
}
img.src = 'http://s3.amazonaws.com/my/image.png'; // I added arbitrary ?crossorigin to change the URL of this to fix it

Chrome已经缓存了另一个版本,从来没有试图重新获取交叉来源版本(即使我在显示的图像上使用交叉来源)。

为了解决这个问题,我添加了?crossorigin到图像url的末尾(但你可以添加?blah,这只是随意改变缓存状态),当我为画布加载它。让我知道,如果你找到一个更好的修复CHROME

Set CORS configuration in Permissions settings for you S3 bucket <?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <MaxAgeSeconds>3000</MaxAgeSeconds> <AllowedHeader>Authorization</AllowedHeader> </CORSRule> </CORSConfiguration> S3 adds CORS headers only when http request has the Origin header. CloudFront does not forward Origin header by default You need to whitelist Origin header in Behavior settings for your CloudFront Distribution.

就像其他人说的,你首先需要在你的S3桶中有CORS配置:

<CORSConfiguration>
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod> <!-- Add this -->
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>

但在我做了这些之后,它仍然不起作用。我用的是Chrome(可能其他浏览器也有同样的问题)。

问题是Chrome是缓存图像的头(不包含CORS数据),所以无论我试图改变AWS,我不会看到我的CORS头。

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

在最新的S3管理控制台中,当您单击Permissions选项卡上的CORS配置时,它将显示一个默认的示例CORS配置。但是,这个配置实际上并没有激活!您必须首先单击save才能激活CORS。

我花了很长时间才弄明白,希望这能帮大家节省一些时间。