google-cloud-platform - 如何使用 Google Cloud Storage 在 CORS json 文件中设置多个来源?

标签 google-cloud-platform cors

我正在尝试将多个 CORS 源站点与 Google Storage 一起使用。

Mozilla Firefox 似乎没有任何多源问题。但是谷歌浏览器抛出这个错误:Access to XMLHttpRequest at 'FILE AT GOOGLE STORAGE' from origin 'https://example2.org' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://example1.org' that is not equal to the supplied origin.
我试过像这样编写 cors json 文件:

[
    {
      "origin": ["https://example1.org", "https://example2.org"],
      "responseHeader": ["Content-Type"],
      "method": ["GET", "HEAD"],
      "maxAgeSeconds": 1800
    } 
]

像这样:
[
    {
      "origin": ["https://example1.org"],
      "responseHeader": ["Content-Type"],
      "method": ["GET", "HEAD"],
      "maxAgeSeconds": 1800
    },
    {
      "origin": ["https://example2.org"],
      "responseHeader": ["Content-Type"],
      "method": ["GET", "HEAD"],
      "maxAgeSeconds": 1800
    }  
]

谷歌浏览器不喜欢这两种变体。

最佳答案

您指定的第一种方式是正确的,保存一个 JSON 文件(即 bucket-cors-config.json ):

[{
  "origin": ["https://example1.org", "https://example2.org"],
  "responseHeader": ["Content-Type"],
  "method": ["GET", "HEAD"],
  "maxAgeSeconds": 1800
}]

然后使用 gcloud cli util 在您的存储桶上设置:

gsutil cors set bucket-cors-config.json gs://my-bucket

如果您在浏览器中检查不同来源并收到 CORS 错误,请确保这不是因为浏览器缓存。由于目标 URL 匹配,一些浏览器会在错误的来源上使用缓存的响应。此缓存响应将具有错误的原始 header 并导致 CORS 错误。

关于google-cloud-platform - 如何使用 Google Cloud Storage 在 CORS json 文件中设置多个来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55226101/

相关文章:

javascript - 网络上的 Firestore 是否对两个听众收取两次读取费用?

google-app-engine - 无法删除版本 - gcloud app versions stop [id]

javascript - 在 AngularJS 中使用 POST 到 .net Web API 时出错

javascript - 谷歌云功能不处理信号

google-cloud-platform - 如何在发布/订阅发布消息上触发 google composer DAG?

ajax - Google Chrome的Advanced REST客户端如何发出跨域POST请求?

java - 将文件上传到 Blob 存储时,Google App Engine 和自定义域出现 CORS 问题

rest - 当当前用户禁止连接点时,我的服务器是否应该为 HTTP OPTIONS 方法返回 200?

python-3.x - 如何在本地对 python 3 应用程序引擎应用程序进行单元测试?

azure - 为政府云上的 Azure Function 添加 CORS 设置