GAE/Standard Python3.7 から blob.generate_signed_url する
Google App Engine Standard Python 3.7 環境にて、GCSに保存したファイルの署名付きURLを生成する方法
解決
GAEとGCSで同じGCP project内なんだから、いい感じにcredentialsを共有してくれると思っていたがそうではないようだ
GAE/Standard Python 2.7 環境ではこれで良かった気がするが...
以下、問題と調査
py
from google.cloud import storage
gcsClient = storage.Client('project-name')
gcsBucket = gcsClient.get_bucket('my-bucket')
blob = gcsBucket.blob('a.jpg')
serving_url = blob.generate_signed_url(...)
なぜ??
GAE環境で実行しているので、credentialsを特に指定しなければ、勝手にdefaultのservice accountが使われるはずでは?
error
AttributeError: 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.
Google App Engine Environment の場合
py
from google.auth import app_engine
credentials = app_engine.Credentials()
gcsClient = storage.Client(credentials=credentials)
のようにすればいい
と書いてあるが、実際にデプロイしてみると
error
OSError: The App Engine APIs are not available.
意味不明。
Google Compute Engine Environment の場合
py
from google.auth import compute_engine
credentials = compute_engine.Credentials()
gcsClient = storage.Client(credentials=credentials)
GAE/Standard環境なのでCompute Engineの話は関係ないと思うが
error
AttributeError: 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://google-cloud-python.readthedocs.io/en/latest/core/auth.html?highlight=authentication#setting-up-a-service-account for more details.
相変わらず同じエラー
service account jsonファイルを直接指定する
py
import os
from google.auth import compute_engine
credentialsPath = os.path.join(os.path.dirname(__file__), '../keys/foo.json')
gcsClient = storage.Client.from_service_account_json(credentialsPath)
動いた