使用Google Cloud Logging(Python)访问GAE日志文件

朱利安

我们有一个正在运行的Google App Engine(GAE)服务,我们想为其下载日志以供我们在服务器上存档。

GAE拥有一个服务帐户,其凭据已作为JSON文件下载到我们的服务器。在我们的服务器上运行的以下代码尝试为日志记录服务创建客户端:

from google.cloud import logging
client = logging.Client.from_service_account_json('credentials.json')

结果:

ValueError: Service account info was not in the expected format, missing fields token_uri, client_email.

错误消息很清楚,但是不清楚为什么在为此目的创建的JSON文件中需要这些字段?我们是否使用来自错误类型的服务帐户的凭据?

忍者

您需要获取包含私钥凭据的服务帐户文件,该文件与您拥有的文件基本上不同。您可以获取它,也可以转到https://console.developers.google.com/iam-admin/iam/,然后选择您的项目,然后选择“服务帐户”并创建一个新角色,以获取一个新的角色。查看器”(例如,使用该项目)(或使用现有的“查看器”,然后单击“创建新密钥”)

“密钥”是一个json或p12文件,当您创建帐户(或在其中使用“创建新密钥”)时将下载该文件,其中包含适用于您代码的正确字段和凭据。

下载的“密钥”文件的示例结构(选择JSON时):

{
  "type": "service_account",
  "project_id": "zeta-handler-9999",
  "private_key_id": "123456789deedbeaf",
  "private_key": "-----BEGIN PRIVATE KEY-----\nREDACTED REDACTED...-----END PRIVATE KEY-----\n",
  "client_email": "projectname-service-account@zeta-handler-9999.iam.gserviceaccount.com",
  "client_id": "12345678909999",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/projectname-service-account%40zeta-handler-9999.iam.gserviceaccount.com"
}

使用该“密钥”文件的示例代码(python):

#!/usr/bin/env python
import google.auth
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file('downloaded_key.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/drive.metadata.readonly'])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Cloud Composer(Apache Airflow)无法访问日志文件

使用Google Cloud Functions加速GAE应用

无法在GAE应用中使用Google Cloud

使用Google Cloud Dataflow合并Google Cloud Storage中的文件

需要帮助以从Google Cloud Platform批量下载日志-Cloud Logging

使用Python从Google Cloud Stackdriver API检索日志名称列表

使用Google Cloud Dataflow生成xml文件

使用Google Cloud功能的Python Flask应用

使用python批量上传到Google Cloud

Google Cloud IoT使用Python订阅主题

在Google Cloud Windows Server上使用python

使用 Python 的 Google Cloud Platform SDK

通过Google Compute Engine和Cloud Logging代理使用日志严重性

使用Google Cloud Logging从一组选定的日志中搜索文本?

Google Cloud Storage 和 Stackdriver Logging:如何使用“gcloud”读取存储桶日志?

如何使用带有 Python 的 Google Cloud Functions 将列表写入 Google Cloud Storage 中的文件

Python Google Cloud 函数缺少日志条目

使用Python 3问题将文件上传到Google Cloud

使用GAE生成API(Google Cloud Endpoint)时出错

使用 Google Cloud SDK shell 在 GAE 中部署

使用 GAE 和 Google Cloud Storage 从 csv 文件上传到存储桶图片

Google Cloud Dataflow:使用DirectPipelineRunner(本地作业)访问管道中的Google Cloud Pub / Sub吗?

使用Google Cloud Console在Google Cloud Storage中的文件夹之间移动文件

使用Google Cloud Storage代替Google Cloud SQL保存文件有什么好处

如何使用Google Cloud Dataflow将压缩文件写入Google Cloud Storage?

从Google Cloud上运行的Cloud访问Cloud SQL

使用 Python 3 使用 Google Cloud Function 重命名对象

Google Cloud Function + Python

在Java中设置Google Cloud Storage Bucket访问日志