简体   繁体   English

Rails ActiveStorage 的 Google Cloud Storage CORS 问题

[英]Google Cloud Storage CORS issue with Rails ActiveStorage

I've set the CORS for my bucket using gsutil with what I believe should work, but I haven't got past the "No 'Access-Control-Allow-Origin' header is present on the requested resource" error.我已经使用 gsutil 为我的存储桶设置了 CORS,我认为应该可以工作,但我还没有解决“请求的资源上不存在‘Access-Control-Allow-Origin’标头”错误。

This is my cors json:这是我的 cors json:

[
  {
    "origin": [
      "*"
    ],
    "responseHeader": [
      "Content-Type",
      "Content-MD5"
    ],
    "method": [
      "PUT",
      "POST",
      "GET",
      "HEAD",
      "DELETE",
      "OPTIONS"
    ],
    "maxAgeSeconds": 3600
  }
]

I've verified that this is actually being set:我已经验证这实际上是被设置的:

gsutil cors get gs://mah-bucket
[{"maxAgeSeconds": 3600, "method": ["PUT", "POST", "GET", "HEAD", "DELETE", "OPTIONS"], "origin": ["*"], "responseHeader": ["Content-Type", "Content-MD5"]}]

These are the provisional headers on the failed request:这些是失败请求的临时标头:

Content-MD5: Ug6Qj+DozqmniNxTXOYnDA==
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Origin: https://subdomain.example.com
Referer: https://subdomain.example.com/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

I've already followed the advice here: https://github.com/rails/rails/issues/31523我已经遵循了这里的建议: https : //github.com/rails/rails/issues/31523

I've tried a fresh incognito window as well so it's not caching anything from previous pre-flight requests.我也尝试了一个新的隐身窗口,因此它不会缓存以前飞行前请求中的任何内容。

Update: I've narrowed the problem down to an edge-case of ActiveStorage with the google service.更新:我已经将问题缩小到使用 google 服务的 ActiveStorage 的边缘情况。 Normal file upload forms work with both Amazon S3 and Google GCS services.普通文件上传表单适用于 Amazon S3 和 Google GCS 服务。 Uploading using the ActiveStorage DirectUploadController js works with Amazon but not with Google.使用ActiveStorage DirectUploadController js 上传适用于 Amazon,但不适用于 Google。

The requests made in each case are identical save for a few auth differences between the services, but the responses to the pre-flight request are different.除了服务之间的一些身份验证差异外,每种情况下发出的请求都是相同的,但对飞行前请求的响应是不同的。 Here's the S3 pre-flight request response:这是 S3 飞行前请求响应:

Access-Control-Allow-Headers: content-md5, content-type
Access-Control-Allow-Methods: PUT, POST, GET, HEAD
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Content-Length: 0
Date: Tue, 24 Jul 2018 22:32:51 GMT
Server: AmazonS3
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2: xxx/x/xxx
x-amz-request-id: xxx

Here's the GCS pre-flight request response:这是 GCS 飞行前请求响应:

access-control-allow-headers: Content-Type,Content-MD5
access-control-allow-methods: PUT,POST,GET,HEAD,DELETE,OPTIONS
access-control-allow-origin: *
access-control-max-age: 3600
alt-svc: quic=":443"; ma=2592000; v="44,43,39,35"
cache-control: private, max-age=0
content-length: 0
content-type: text/html; charset=UTF-8
date: Tue, 24 Jul 2018 22:43:25 GMT
expires: Tue, 24 Jul 2018 22:43:25 GMT
server: UploadServer
status: 200
x-guploader-uploadid: xxx-xxx

As stupid as it seems, this appears to be a case-sensitivity issue.尽管看起来很愚蠢,但这似乎是一个区分大小写的问题。

It turns out that the version of the direct_uploads_controller.js file I had copied out of Rails had a number of issues with browser and service compatibility. 事实证明,我从Rails中复制的direct_uploads_controller.js文件的版本在浏览器和服务兼容性方面存在许多问题。 I copied over all the files in this folder and installed the missing spark-md5 package and all is well now: https://github.com/rails/rails/tree/master/activestorage/app/javascript/activestorage 我复制了该文件夹中的所有文件,并安装了缺少的spark-md5包,现在一切都很好: https : //github.com/rails/rails/tree/master/activestorage/app/javascript/activestorage

The commit I've tested is 372dda2a2950ad3ae5cf744ed8e3caa69a7ed44b. 我测试过的提交是372dda2a2950ad3ae5cf744ed8e3caa69a7ed44b。

If anyone is still stuck with this issue, the CORS configuration is missing Origin and Content-Disposition response headers.如果有人仍然遇到此问题,则 CORS 配置缺少OriginContent-Disposition响应标头。 It was the case for me, at least.至少对我来说是这样。

Therefore, the configuration should be as follows:因此,配置应如下所示:

[
  {
    "origin": ["https://www.example.com"],
    "method": ["PUT"],
    "responseHeader": ["Origin", "Content-Type", "Content-MD5", "Content-Disposition"],
    "maxAgeSeconds": 3600
  }
]

Source: https://guides.rubyonrails.org/active_storage_overview.html#example-google-cloud-storage-cors-configuration来源: https : //guides.rubyonrails.org/active_storage_overview.html#example-google-cloud-storage-cors-configuration

Are you using literally: 您是按字面意思使用吗?

"https://subdomain.example.com",
"https://example.com",
"https://*.example.com"

as origins? 作为起源? if so, that may be the problem. 如果是这样,那可能是问题所在。 You are probably not calling your gs bucket from any of these domains. 您可能没有从任何这些域中调用gs存储桶。 You should have the domain from which you are calling it. 您应该具有从中调用它的域。 including localhost:port if testing from there 如果从那里进行测试,则包括localhost:port

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM