GCP:在本地创建 Client() 对象

GCP: Creating Client() object locally

我有这个 class 用于记录到 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)

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

我的整个代码都崩溃了。

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

问题:有什么方法可以在本地实现吗?它应该工作。我给它信用和项目,根据 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

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

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