小编sww*_*314的帖子

如何在 Google Cloud Run 中生成 Blob 签名 url?

在 Google Cloud Run 下,您可以选择容器正在运行的服务帐户。使用默认计算服务帐户无法生成签名 url。

此处列出的解决方法适用于 Google Cloud Compute - 如果您允许服务帐户的所有范围。在 Cloud Run 中似乎没有办法做到这一点(我找不到)。

https://github.com/googleapis/google-auth-library-python/issues/50

我尝试过的事情:

  1. 为服务帐号分配角色: roles/iam.serviceAccountTokenCreator
  2. 在虚拟机(vs Cloud Run)中验证了同一个 GCP 项目中的解决方法
  3. 使用从私钥(通过 json 文件)加载的服务帐户验证代码在容器中本地工作。
from google.cloud import storage
client = storage.Client()
bucket = client.get_bucket('EXAMPLE_BUCKET')
blob = bucket.get_blob('libraries/image_1.png')
expires = datetime.now() + timedelta(seconds=86400)
blob.generate_signed_url(expiration=expires)
Run Code Online (Sandbox Code Playgroud)

失败:

you need a private key to sign credentials.the credentials you are currently using <class 'google.auth.compute_engine.credentials.Credentials'> just contains a token. see https://googleapis.dev/python/google-api-core/latest/auth.html#setting-up-a-service-account for more details.
/usr/local/lib/python3.8/site-packages/google/cloud/storage/_signing.py, line 51, in ensure_signed_credentials
Run Code Online (Sandbox Code Playgroud)

尝试添加解决方法,

Error calling the …
Run Code Online (Sandbox Code Playgroud)

python google-cloud-storage google-iam google-cloud-run

3
推荐指数
1
解决办法
1383
查看次数