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

2024-04-23 21:14:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类,用于记录到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"))处中断

我的密码全断了。你知道吗

问题:如果不在云上,有没有方法跳过实例化这个类?像条件类?你知道吗

问题:有什么方法可以让这项工作在本地进行吗?应该有用。我给它creds和项目,根据StackDriver docs如果我给它project+creds,它应该在本地和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

Tags: frompyselfprojectclientcloudinitlogging