在 Google Cloud Run 下,您可以选择容器正在运行的服务帐户。使用默认计算服务帐户无法生成签名 url。
此处列出的解决方法适用于 Google Cloud Compute - 如果您允许服务帐户的所有范围。在 Cloud Run 中似乎没有办法做到这一点(我找不到)。
https://github.com/googleapis/google-auth-library-python/issues/50
我尝试过的事情:
roles/iam.serviceAccountTokenCreatorfrom 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)