有人设法在响应头中添加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头

其他回答

我在加载web字体时遇到了类似的问题,当我点击“添加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>HEAD</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>
</CORSConfiguration> 

我只是点击保存,它工作得很好,我的自定义网页字体正在IE和Firefox中加载。我不是这方面的专家,我只是觉得这个也许能帮到你。

S3现在期望规则是Array Json格式。

你可以在s3桶中找到这个->权限然后->向下滚动->()跨起源资源共享(CORS)

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
    }
]

这是一种简单的工作方法。

我知道这是一个老问题,但仍然很难找到解决办法。

首先,我用Rails 4、Paperclip 4、CamanJS、Heroku和AWS S3构建了一个项目。


你必须使用crossorigin: "anonymous"参数请求你的图像。

    <img href="your-remote-image.jpg" crossorigin="anonymous"> 

Add your site URL to CORS in AWS S3. Here is a refference from Amazon about that. Pretty much, just go to your bucket, and then select "Properties" from the tabs on the right, open "Permissions tab and then, click on "Edit CORS Configuration". Originally, I had < AllowedOrigin> set to *. Just change that asterisk to your URL, be sure to include options like http:// and https:// in separate lines. I was expecting that the asterisk accepts "All", but apparently we have to be more specific than that.

对我来说是这样的。

<AllowedOrigin>*</AllowedOrigin>

这不是一个好主意,因为使用*将授予任何网站访问bucket中的文件的权限。相反,您应该指定确切允许哪个源使用bucket中的资源。通常,这是你的域名

<AllowedOrigin>https://www.example.com</AllowedOrigin>

或者如果你想包含所有可能的子域:

<AllowedOrigin>*.example.com</AllowedOrigin>

我只想在上面加上一个答案——这解决了我的问题。

要将AWS/CloudFront分发点设置为朝向CORS源标头,请单击进入分发点的编辑界面:

进入行为选项卡并编辑行为,将“基于所选请求头的缓存”从无更改为白名单,然后确保将Origin添加到白名单框中。有关更多信息,请参阅AWS文档中的配置CloudFront以尊重CORS设置。