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


当前回答

我也遇到了类似的问题,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参数的更多信息。

其他回答

在我的例子中,我用下面的配置来解决它 首先,进入权限,然后进入跨源资源共享(CORS) 然后点击编辑并粘贴下面的代码…

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "http://www.example1.com"
        ],
        "ExposeHeaders": []
    },
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "http://www.example2.com"
        ],
        "ExposeHeaders": []
    },
    {
        "AllowedHeaders": [],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]

点击这里了解更多信息

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

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

首先,我用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.

对我来说是这样的。

我尝试了上面所有的答案,没有一个工作。实际上,我们需要从上述答案中总结出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.

对我来说,不加区域

Const s3 = new aws。S3({apiVersion: '2006-03-01',区域:'us-west-2'});

@jordanstephens在评论中说过,但它有点丢失了,对我来说真的很容易修复。

我简单地添加HEAD方法并单击保存,它开始工作。

< CORSConfiguration > < CORSRule > * < AllowedOrigin > < / AllowedOrigin > < AllowedMethod > < / AllowedMethod弄> < AllowedMethod > < / AllowedMethod > <头!添加这个-> < 3000 MaxAgeSeconds > < / MaxAgeSeconds > < AllowedHeader > Authorization < / AllowedHeader > < / CORSRule > < / CORSConfiguration >