我正在尝试将多个 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/