【问题标题】:Cloud Storage Signed URLs By ProxyCloud Storage 通过代理签名的 URL
【发布时间】:2022-06-14 06:57:34
【问题描述】:

我正在尝试为 Cloud Run(和 App Engine)项目支持大文件上传。有一些限制会阻止通常的解决方法发挥作用:

  • 客户端是 .NET 4.0 应用程序,这意味着 HTTP2 不可用(这至少可以让您绕过 Cloud Run 的 32MB 请求大小限制)
  • 旧版客户端不可升级,因此无法进行分块上传,并且需要向后兼容
  • 云存储的签名 URL 是当前的解决方案并且运行良好,但是由于客户的 IT 阻止了 googleapis(但不是我们的公司域),部分客户根本无法工作
  • 要求客户的 IT 解除对 googleapis 的阻止是困难的/不适合

这使我得出结论,我应该设置一个转发代理,允许签名 URL 通过我们的 GCP 项目/公司域绕过 IT 限制。我会在 Compute Engine 中使用运行 nginx 或 squid 或其他东西的实例来完成此操作,然后让负载均衡器将特定模式的 URL 定向到转发代理,该代理会将 URL 重写为正确的云存储签名 URL 并转发请求。

但是,这似乎是一个有点笨拙的解决方案。是否有更简单的 GCP 原生功能可以完成我想要做的事情?

【问题讨论】:

  • 在我看来,带有存储桶后端的云负载均衡器已经涵盖了这个用例 cloud.google.com/load-balancing/docs/https/… ,您可能还需要云 CDN 进行身份验证 cloud.google.com/cdn/docs/using-signed-urls 不作为答案发布,因为我从来没有使用其中任何一个,所以不确定它是否涵盖了所有内容。
  • GCP 负载均衡器似乎不允许您执行简单的代理传递 - 它使您可以从现有后端服务的下拉列表中进行选择。在我的情况下,我通过连接到一个 GCE 实例组来使其工作,该实例组仅使用代理传递配置运行 nginx。如果有人感兴趣,我会发布我的 nginx conf 文件作为答案。

标签: google-app-engine google-cloud-platform google-cloud-storage google-cloud-run


【解决方案1】:

我能够使用 nginx 代理云存储签名的 URL:

events {
  worker_connections 1024;
}

http {
 client_max_body_size 500M;

 server {
   listen 80;
   listen [::]:80;

   server_name mydomain;

    location /storagepxy/ {
        proxy_pass https://storage.googleapis.com/;
    }
  }
}

然后我设置了一个 GCP 负载均衡器,以将任何以 /storagepxy/* 开头的请求定向到使用上述配置运行 nginx 的计算引擎实例组。

因此,我可以使用以下形式的请求读取/写入云存储:

GET mydomain/storagepxy/[cloud storage signeduri]
PUT mydomain/storagepxy/[cloud storage signeduri]

如果你有一个签名的 URL,比如:

https://storage.googleapis.com/example-bucket/cat.jpeg?X-Goog-Algorithm=
GOOG4-RSA-SHA256&X-Goog-Credential=example%40example-project.iam.gserviceaccount
.com%2F20181026%2Fus-central1%2Fstorage%2Fgoog4_request&X-Goog-Date=20181026T18
1309Z&X-Goog-Expires=900&X-Goog-SignedHeaders=host&X-Goog-Signature=247a2aa45f16
9edf4d187d54e7cc46e4731b1e6273242c4f4c39a1d2507a0e58706e25e3a85a7dbb891d62afa849
6def8e260c1db863d9ace85ff0a184b894b117fe46d1225c82f2aa19efd52cf21d3e2022b3b868dc
c1aca2741951ed5bf3bb25a34f5e9316a2841e8ff4c530b22ceaa1c5ce09c7cbb5732631510c2058
0e61723f5594de3aea497f195456a2ff2bdd0d13bad47289d8611b6f9cfeef0c46c91a455b94e90a
66924f722292d21e24d31dcfb38ce0c0f353ffa5a9756fc2a9f2b40bc2113206a81e324fc4fd6823
a29163fa845c8ae7eca1fcf6e5bb48b3200983c56c5ca81fffb151cca7402beddfc4a76b13344703
2ea7abedc098d2eb14a7

您可以通过以下方式代理它:

https://mydomain/storagepxy/example-bucket/cat.jpeg?X-Goog-Algorithm=
GOOG4-RSA-SHA256&X-Goog-Credential=example%40example-project.iam.gserviceaccount
.com%2F20181026%2Fus-central1%2Fstorage%2Fgoog4_request&X-Goog-Date=20181026T18
1309Z&X-Goog-Expires=900&X-Goog-SignedHeaders=host&X-Goog-Signature=247a2aa45f16
9edf4d187d54e7cc46e4731b1e6273242c4f4c39a1d2507a0e58706e25e3a85a7dbb891d62afa849
6def8e260c1db863d9ace85ff0a184b894b117fe46d1225c82f2aa19efd52cf21d3e2022b3b868dc
c1aca2741951ed5bf3bb25a34f5e9316a2841e8ff4c530b22ceaa1c5ce09c7cbb5732631510c2058
0e61723f5594de3aea497f195456a2ff2bdd0d13bad47289d8611b6f9cfeef0c46c91a455b94e90a
66924f722292d21e24d31dcfb38ce0c0f353ffa5a9756fc2a9f2b40bc2113206a81e324fc4fd6823
a29163fa845c8ae7eca1fcf6e5bb48b3200983c56c5ca81fffb151cca7402beddfc4a76b13344703
2ea7abedc098d2eb14a7

注意:如果您的存储桶路径包含 URL 编码的字符(例如冒号),则需要稍微复杂的 nginx 配置:

# This is a simple nginx configuration file that will proxy URLs of the form:
#   https://autocontour.radformation.com/storagepxy/[signed uri]
# to
#   https://storage.googleapis.com/[signed uri]
#
# For use in GCP, you'll likely need to create an instance group in compute engine running nginx with this config
# and then hook up a load balancer to forward requests starting with /storagepxy to it
worker_processes auto; # Auto should spawn 1 worker per core

events {}
http {
  client_max_body_size 500M;

  server {
    listen 80; # IPv4
    listen [::]:80; # IPv6
    server_name mydomain;

    location /storagepxy/ {
      # To resolve storage.googleapis.com
      resolver 8.8.8.8;

      # We have to do it this way in case filenames have URL-encoded characters in them
      # See: https://stackoverflow.com/a/37584637
      # Also note, if the URL does not match the rewrite rules then return 400
      rewrite ^ $request_uri;
      rewrite ^/storagepxy/(.*) $1 break;
      return 400;

      proxy_pass https://storage.googleapis.com/$uri;
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 2017-04-03
    • 2016-05-16
    • 2017-07-06
    • 1970-01-01
    相关资源
    最近更新 更多