【问题标题】:Google Cloud Storage CORS issue with Rails ActiveStorageRails ActiveStorage 的 Google Cloud Storage CORS 问题
【发布时间】:2018-12-31 11:12:38
【问题描述】:

我已经使用 gsutil 为我的存储桶设置了 CORS,我认为它应该可以工作,但我还没有解决“请求的资源上不存在 'Access-Control-Allow-Origin' 标头”错误。

这是我的 cors json:

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

我已经验证这实际上是在设置:

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

这些是失败请求的临时标头:

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

我已经听从了这里的建议:https://github.com/rails/rails/issues/31523

我也尝试了一个新的隐身窗口,因此它不会缓存之前飞行前请求的任何内容。

更新: 我已将问题缩小到使用 google 服务的 ActiveStorage 的边缘情况。普通文件上传表单适用于 Amazon S3 和 Google GCS 服务。使用ActiveStorage DirectUploadController js 上传适用于亚马逊,但不适用于谷歌。

除了服务之间的一些身份验证差异外,在每种情况下发出的请求都是相同的,但对飞行前请求的响应不同。这是 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

这是 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

尽管看起来很愚蠢,但这似乎是一个区分大小写的问题。

【问题讨论】:

  • 由于这是'rails gcs cors' issue的第一个搜索结果,我想说guideguides.rubyonrails.org/…中的示例文件与OP不同。

标签: amazon-s3 gcloud rails-activestorage


【解决方案1】:

事实证明,我从 Rails 中复制的 direct_uploads_controller.js 文件版本在浏览器和服务兼容性方面存在许多问题。我复制了这个文件夹中的所有文件并安装了缺少的 spark-md5 包,现在一切都很好: https://github.com/rails/rails/tree/master/activestorage/app/javascript/activestorage

我测试的提交是 372dda2a2950ad3ae5cf744ed8e3caa69a7ed44b。

【讨论】:

    【解决方案2】:

    如果有人仍然遇到此问题,则缺少 CORS 配置 OriginContent-Disposition 响应标头。至少对我来说是这样。

    因此,配置应该如下:

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

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

    【讨论】:

      【解决方案3】:

      你是按字面意思使用吗:

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

      作为起源?如果是这样,那可能就是问题所在。您可能没有从这些域中的任何一个调用您的 gs 存储桶。您应该拥有从中调用它的域。如果从那里进行测试,则包括 localhost:port

      【讨论】:

      • 不是字面意思,不是。我正在使用一个真实的域。不过,我已经尝试过每一次迭代。我现在只有subdomain.example.com(相当于),它与请求中的来源和浏览器控制台中的location.origin完全匹配。
      猜你喜欢
      • 2016-01-19
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 2019-04-13
      • 2017-12-18
      • 2016-02-06
      • 1970-01-01
      相关资源
      最近更新 更多