因此,我尝试使用以下方法为我的 Google Cloud Storage 对象生成临时的全局可读 URLgoogle-cloud-storage
Python 库(https://googlecloudplatform.github.io/google-cloud-python/latest/storage/blobs.html https://googlecloudplatform.github.io/google-cloud-python/latest/storage/blobs.html) - 更具体地说是 Blob.generate_signed_url() 方法。我在命令行 Python 脚本的 Compute Engine 实例中执行此操作。我不断收到以下错误:
AttributeError: you need a private key to sign credentials.the credentials you are currently using <class 'oauth2cl
ient.service_account.ServiceAccountCredentials'> 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.
我知道在 GCE 内部执行此操作存在问题(https://github.com/GoogleCloudPlatform/google-auth-library-python/issues/50 https://github.com/GoogleCloudPlatform/google-auth-library-python/issues/50)但我按照此处的说明创建了一个新的服务帐户凭据:https://cloud.google.com/storage/docs/access-control/create-signed-urls-program https://cloud.google.com/storage/docs/access-control/create-signed-urls-program我的 key.json 文件肯定包含私钥。我仍然看到那个错误。
这是我的代码:
keyfile = "/path/to/my/key.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(keyfile)
expiration = timedelta(3) # valid for 3 days
url = blob.generate_signed_url(expiration, method="GET",
credentials=credentials)
我已阅读此处的问题跟踪器并且没有任何相关的内容跳出来,所以我认为这应该可行。看不出这里出了什么问题。
我也有同样的问题。最终通过直接从服务帐户 json 启动存储客户端来修复它。
storage_client = storage.Client.from_service_account_json('path_to_service_account_key.json')
我知道我参加聚会迟到了,但希望这会有所帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)