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


当前回答

我尝试了上面所有的答案,没有一个工作。实际上,我们需要从上述答案中总结出3个步骤来实现它:

As suggested by Flavio; add CORS configuration on your bucket: <CORSConfiguration> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> </CORSRule> </CORSConfiguration> On the image; mention crossorigin: <img href="abc.jpg" crossorigin="anonymous"> Are you using a CDN? If everything works fine connecting to origin server but NOT via CDN; it means you need some setting on your CDN like accept CORS headers. Exact setting depends on which CDN you are using.

其他回答

S3现在要求它以Javascript对象符号格式出现。

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

我建议根据您的需求更改AllowedOrigins和AllowedMethods。

我尝试了上面所有的答案,没有一个工作。实际上,我们需要从上述答案中总结出3个步骤来实现它:

As suggested by Flavio; add CORS configuration on your bucket: <CORSConfiguration> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> </CORSRule> </CORSConfiguration> On the image; mention crossorigin: <img href="abc.jpg" crossorigin="anonymous"> Are you using a CDN? If everything works fine connecting to origin server but NOT via CDN; it means you need some setting on your CDN like accept CORS headers. Exact setting depends on which CDN you are using.

我也遇到了类似的问题,coderVishal的回答帮助我解决了这个问题,但在我的情况下,我需要在下一个配置中使用Terraform:

resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket
  acl    = "public-read"

  # Cross-origin resource sharing (CORS)
  cors_rule {
    allowed_headers = ["*"]
    allowed_methods = ["GET", "HEAD"]
    allowed_origins = ["*"]
    expose_headers  = []
    max_age_seconds = 3000
  }
}

在文档中阅读关于cors_rule参数的更多信息。

公认的答案是可行的,但如果直接访问资源,似乎就不存在跨源头。如果您正在使用cloudfront,这将导致cloudfront缓存没有标头的版本。当你转到一个不同的url来加载这个资源时,你会遇到这个跨源问题。

我在加载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中加载。我不是这方面的专家,我只是觉得这个也许能帮到你。