【问题标题】:What is suggested method to get service versions获取服务版本的建议方法是什么
【发布时间】:2020-01-19 22:20:05
【问题描述】:

在 flex env 中获取谷歌应用引擎中服务版本列表的最佳方法是什么? (来自 Python 3 中的服务实例)。我想使用服务帐户 json 密钥文件进行身份验证。我需要找到当前的默认版本(流量最多)。

有没有我可以使用的库,例如 googleapiclient.discoverygoogle.appengine.api.modules?或者我应该从头开始构建它并使用 oauth 在apps.services.versions.list 上请求 REST api?我在谷歌文档中找不到任何信息.. https://cloud.google.com/appengine/docs/standard/python3/python-differences#cloud_client_libraries

【问题讨论】:

    标签: python-3.x google-app-engine-python app-engine-flexible


    【解决方案1】:

    您可以使用 Google 的 Python Client Library 与 Google App Engine Admin API 进行交互,以获取 GAE 服务版本列表。

    安装 google-api-python-client 后,您可能希望使用 list 方法列出应用程序中的所有服务:

    list(appsId, pageSize=None, pageToken=None, x__xgafv=None)

    方法的参数应包括以下内容:

    • appsId字符串,“name”的一部分。请求的资源的名称。示例:应用程序/myapp。 (必填)
    • pageSize整数,每页返回的最大结果。
    • pageTokenstring,用于获取下一页结果的继续令牌。
    • x__xgafv字符串,V1 错误格式。允许的v1 错误格式v2 错误格式

    您可以在上述链接中找到有关此方法的更多信息。

    【讨论】:

    • 谢谢,是的,这就是我现在想要实现的。 service = build('appengine', 'v1', credentials=creds) data = service.apps().services().get(appsId=APPLICATION_ID, name=SERVICE_ID).execute() 我可以使用服务帐户 json 和 InstalledAppFlow.from_client_secrets_file() 进行身份验证吗?或者有更简单的选择,也许从我不需要验证的 gae 实例发出请求?
    【解决方案2】:

    最后我能够解决它。 GAE 上的简单事情变成了大问题..

    解决方案: 我在GOOGLE_APPLICATION_CREDENTIALS env 变量中设置了 service_account.json 的路径。然后你可以使用google.auth.default

    from googleapiclient.discovery import build
    import google.auth
    
    creds, project = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform.read-only'])
    service = build('appengine', 'v1', credentials=creds, cache_discovery=False)
    data = service.apps().services().get(appsId=APPLICATION_ID, servicesId=SERVICE_ID).execute()
    print data['split']['allocations']
    

    返回值是分配字典,其中版本为键,流量百分比为值。 万事如意!

    【讨论】:

      猜你喜欢
      • 2020-05-25
      • 2011-09-10
      • 2018-02-16
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多