【问题标题】:GCP: Creating Client() object locallyGCP:在本地创建 Client() 对象
【发布时间】:2020-04-22 00:04:00
【问题描述】:

我有这个类用于记录到 StackDriver。

from google.cloud.logging.handlers import CloudLoggingHandler
from google.oauth2 import service_account

class GoogleLogger(CloudLoggingHandler):

    CREDS = google.cloud.logging.Client(
        project=PROJECT, credentials=service_account.Credentials.from_service_account_file("/path/to/creds"))

    def __init__(self, client=CREDS):
        super(GoogleLogger, self).__init__(client)

在谷歌云上运行时,可以无缝运行。但是,当在本地运行时,它会在 CREDS = google.cloud.logging.Client(project=PROJECT, credentials=service_account.Credentials.from_service_account_file("/path/to/creds")) 处中断

我的整个代码都中断了。

问题:如果不在云端,有什么方法可以跳过实例化这个类?像一个条件类?

问题:有没有办法在本地进行这项工作?它应该工作。我给它信用和项目,根据StackDriver docs,如果我给它项目+信用,它应该在本地以及在 GCP 中工作?

当它中断时,这是回溯:

Traceback (most recent call last):
  File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 27, in <module>
    class GoogleLogger(CloudLoggingHandler):
  File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 36, in GoogleLogger
    project=PROJECT, credentials=service_account.Credentials.from_service_account_file(local_or_gcp()))
  File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/client.py", line 123, in __init__
    self._connection = Connection(self, client_info=client_info)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/_http.py", line 39, in __init__
    super(Connection, self).__init__(client, client_info)
TypeError: __init__() takes 2 positional arguments but 3 were given

【问题讨论】:

  • 当它中断时你会遇到什么错误?
  • @MicahCarrick 编辑并添加回溯到问题。
  • 您运行的是哪个版本的google-cloud-corepip show google-cloud-core
  • Version: 0.24.1
  • @MicahCarrick 我会更新这个,然后回复你。我觉得您可能是正确的,因为我的计算引擎实例上安装了 1.1 版。

标签: python python-3.x logging google-cloud-platform stackdriver


【解决方案1】:

Google 在此更改中添加了client_info 参数:https://github.com/googleapis/google-cloud-python/pull/7849/files#diff-340196d499e9d0eea25cd457f53bfa42L31

我怀疑您在 GCP 环境中运行的是新版本的 Google Cloud Python SDK,而在本地环境中运行的是旧版本。

【讨论】:

  • 现在测试!
  • 谢谢,我没想到要检查版本,这是修复!
猜你喜欢
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多