简体   繁体   English

如何使用 Python3.7 在 Google App Engine 标准环境中为 Google 云存储签名 url?

[英]How to signed url for Google cloud storage on Google App Engine Standard environment with Python3.7?

I can't find a way to have a working signed url on Google App Engine Standard environment with Python3.7.我找不到使用 Python3.7 在 Google App Engine 标准环境中使用签名网址的方法。

I have look at the documentation here : https://cloud.google.com/storage/docs/access-control/signing-urls-manually我在这里查看文档: https : //cloud.google.com/storage/docs/access-control/signing-urls-manually

Within a Google App Engine application, you can use the App Engine App Identity service to sign your string.在 Google App Engine 应用程序中,您可以使用App Engine App Identity服务来签署您的字符串。

But the App Engine App Identity rely on google.appengine package, that is not availalble on python 3.7 env as explain here但是 App Engine App Identity 依赖于google.appengine包,这在 python 3.7 env 上不可用,如解释here

Proprietary App Engine APIs are not available in Python 3. This section lists recommended replacements.专有 App Engine API 在 Python 3 中不可用。本节列出了推荐的替代品。

The overall goal is that your app should be fully portable and run in any standard Python environment.总体目标是您的应用程序应该是完全可移植的,并且可以在任何标准 Python 环境中运行。 You write a standard Python app, not an App Engine Python app.您编写的是标准 Python 应用程序,而不是 App Engine Python 应用程序。 As part of this shift, you are no longer required to use proprietary App Engine APIs and services for your app's core functionality.作为这一转变的一部分,您不再需要为应用的核心功能使用专有的 App Engine API 和服务。 At this time, App Engine APIs are not available in the Python 3.7 runtime.目前,App Engine API 在 Python 3.7 运行时中不可用。

All the api on sdk rely on google.appengine and raise an exception on python 3.7 env : EnvironmentError('The App Engine APIs are not available.') raise here that rely on proprietary api : sdk 上的所有 api 都依赖于google.appengine并在 python 3.7 env 上引发异常: EnvironmentError('The App Engine APIs are not available.') 在此处引发依赖专有 api 的异常:

try:
    from google.appengine.api import app_identity
except ImportError:
    app_identity = None

I know I can use many solution like ServiceAccountCredentials.from_json_keyfile_dict(service_account_dict) but I have to upload a file with credentials directly on app engine and I can't do it since the project credential will be expose on git or ci.我知道我可以使用许多解决方案,例如ServiceAccountCredentials.from_json_keyfile_dict(service_account_dict)但我必须直接在应用引擎上上传带有凭据的文件,我不能这样做,因为项目凭据将在 git 或 ci 上公开。

I really want to rely on default credential from app engine like other Google Cloud api like storage.Client() for example that work out of box.我真的很想依赖来自应用引擎的默认凭据,比如其他 Google Cloud api,比如storage.Client() ,例如开箱即用。

Any suggestion ?有什么建议吗?

For Python interactions with Google Cloud use Python Client that is supported on App Engine standard Python 3 runtime. 对于Python与Google Cloud的交互,请使用App Engine标准Python 3运行时支持的Python客户端

To access Cloud Storage using google-cloud-storage from App Engine Standard: 要使用App Engine Standard中的google-cloud-storage访问云存储:

  1. Add dependency to the requirements.txt > google-cloud-storage==1.14.0 添加依赖项到requirements.txt> google-cloud-storage == 1.14.0
  2. Use Storage Client library , authenticating with storage.Client() only. 使用Storage Client库 ,仅使用storage.Client()身份验证。

Depending on what you need to achieve, I would also suggest trying different possible approaches: 根据您需要实现的目标,我还建议您尝试不同的方法:

  1. Allow anonymous access for public data stored in the bucket. 允许匿名访问存储在存储桶中的公共数据。
  2. For signed URL API call use Method: projects.serviceAccounts.signBlob . 对于签名的URL API调用,请使用方法:projects.serviceAccounts.signBlob Documentation includes examples: 文档包括示例:

It is also possible to sign blobs with appengine api using: 也可以使用appengine apiblob进行签名

google.appengine.api.app_identity.sign_blob()

This question might be old, but it's one the first ones to show on a Google search, so I thought it might help someone who comes looking in the future to post this here as well.这个问题可能很老,但它是第一个在 Google 搜索中显示的问题,所以我认为它可能会帮助将来寻找的人也在这里发布这个问题。

The answer @guillaume-blaquiere posted here does work, but it requires an additional step not mentioned, which is to add the Service Account Token Creator role in IAM to your default service account , which will allow said default service account to "Impersonate service accounts (create OAuth2 access tokens, sign blobs or JWTs, etc)."此处发布的答案@guillaume-blaquiere确实有效,但它需要一个未提及的额外步骤,即将 IAM 中的Service Account Token Creator角色添加到您的默认服务帐户,这将允许所述默认服务帐户“模拟服务帐户” (创建 OAuth2 访问令牌、签署 blob 或 JWT 等)。”

This allows the default service account to sign blobs, as per the signBlob documentation .这允许默认服务帐户根据signBlob 文档对 blob 进行签名。

I tried it on AppEngine and it worked perfectly once that permission was given.我在 AppEngine 上尝试过,一旦获得许可,它就可以完美运行。

import datetime as dt

from google import auth
from google.cloud import storage

# SCOPES = [
#     "https://www.googleapis.com/auth/devstorage.read_only",
#     "https://www.googleapis.com/auth/iam"
# ]

credentials, project = auth.default(
#     scopes=SCOPES
)
credentials.refresh(auth.transport.requests.Request())

expiration_timedelta = dt.timedelta(days=1)

storage_client = storage.Client(credentials=credentials)
bucket = storage_client.get_bucket("bucket_name")
blob = bucket.get_blob("blob_name")

signed_url = blob.generate_signed_url(
    expiration=expiration_timedelta,
    service_account_email=credentials.service_account_email,
    access_token=credentials.token,
)

I downloaded a key for the AppEngine default service account to test locally, and in order to make it work properly outside of the AppEngine environment, I had to add the proper scopes to the credentials, as per the commented lines setting the SCOPES .我下载了 AppEngine默认服务帐户的密钥以进行本地测试,为了使其在 AppEngine 环境之外正常工作,我必须根据设置SCOPES的注释行向凭据添加适当的范围。 You can ignore them if running only in AppEngine itself.如果仅在 AppEngine 本身中运行,您可以忽略它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 改进在Python 3.7标准环境下运行Django的Google App Engine的冷启动时间 - Improving cold start up times on Google App Engine running Django on Python 3.7 Standard environment 在谷歌应用引擎和 ndb 数据存储中从 python2.7 升级到 python3.7 - upgrading from python2.7 to python3.7 in google app engine and ndb datastore 从 python3.7 运行时连接到 Google App Engine 共享内存缓存 - Connect to Google App Engine shared memcached from python3.7 runtime Google Cloud Engine-如何在Python 3.7中接收电子邮件并处理? - Google Cloud Engine - How to receive an email and handle in Python 3.7? 无法使用谷歌云日志记录在python3标准环境的谷歌应用引擎中打印相关日志的模块名称和行号 - Unable to print module name and line number for correlated logs in google app engine fo python3 standard environment using google cloud logging 如何在python 3 Google App Engine标准环境中进行后台工作? - How can I do background work in python 3 Google App Engine standard environment? 如何在python3标准应用引擎项目中访问谷歌云库? - How to access Google cloud library in python3 standard app engine project? 谷歌云存储上传方法返回签名的 URL。 Django python - Google Cloud storage upload method is returning signed URL. Django python Google App Engine (Python 3.7) 上的自定义 SMTP 服务器 - Custom SMTP server on Google App Engine (Python 3.7) 与 Stripe 的网络通信失败 - Google App Engine Python 3.7 - Network communication with Stripe failed - Google App Engine Python 3.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM