云存储 - AngularJS 视图请求的资源上不存在“Access-Control-Allow-Origin”标头

Jam*_*ker 12 cross-domain cors google-cloud-storage gsutil google-cloud-platform

根据 Chrome 开发工具,我获取 html 部分的请求具有源头https://site-name-here.com和请求头 GET。
我的存储桶设置了以下 JSON 文件:

[
    {
      "origin": ["https://site-name-here.com"],
      "responseHeader": ["content-type"],
      "method": ["GET"],
      "maxAgeSeconds": 3600
    }
]
Run Code Online (Sandbox Code Playgroud)

但是,每当 angular 尝试获取视图时,我都会收到以下错误: XMLHttpRequest cannot load https://storage.googleapis.com/bucket-name/view-path.html?. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://site-name-here.com' is therefore not allowed access.

我还提供来自 Cloud Storage 的 javascript 和 css 文件,但它们工作正常,我假设是因为 CSS 没有 CORS 限制,而且我在 Angular 中使用 $sceDelegateProvider.resourceUrlWhitelist() 作为脚本?

编辑:现在工作,我假设有些东西被缓存了,它为我提供了一个旧版本。

小智 6

只是为了支持@Idcmp 的回答,CORS 政策确实需要一段时间才能完全生效(这没有记录在案)。我花了大约 2 个小时试图弄清楚为什么即使我使用以下策略仍然在浏览器中出现 CORS 错误(强烈不推荐这样做,但对我的个人测试项目来说很好)。

{
    "cors": [
      {
        "origin": [
          "*"
        ],
        "method": [
          "*"
        ],
        "responseHeader": [
          "*"
        ],
        "maxAgeSeconds": 3600
      }
    ]
}  
Run Code Online (Sandbox Code Playgroud)

ps 应用新的 CORS 政策后休息一下,你绝对值得


Idc*_*cmp 5

作为通过 Google 找到此问题的人的总结,应用 CORS 策略似乎需要一些时间才能生效。上面的答案表明这可能是由于中间缓存等造成的——这些事情可能超出了您的直接控制范围。

如果可以,请应用gsutil允许通配符来源的 CORS 策略(通过 )并通过https://www.test-cors.org/测试您的请求,否则curl --verbose --output /dev/null -H "Origin: https://your.actual.origin.here" https://storage.googleapis.com/bucket/object您最终将看到您正在寻找的标头:

< access-control-allow-origin: *
< access-control-expose-headers: Content-Length, Date, Server, Transfer-Encoding, X-GUploader-UploadID, X-Google-Trace
Run Code Online (Sandbox Code Playgroud)

一旦它起作用,限制您实际使用的来源和方法,这样您就不会出现新闻和/或产生令人惊讶的大带宽费用。

如果您已完成此操作但仍然遇到问题,请尝试将具有新名称的对象上传到存储桶(即 100% 未缓存在任何地方)进行测试。


小智 3

事实上,此类问题可以通过清除浏览器或其他中间服务\xe2\x80\x99s 缓存来解决。

\n\n

如果您的应用程序在 Google App Engine 实例上运行,则可以通过重新部署新版本的应用程序来清除其中间缓存。或者,也可以设置在 Web 代理和浏览器中缓存的一个default_expiration或多个expiration文件(请参阅静态缓存过期部分有关更多信息,请参阅 app.yaml 参考的

\n