gpt4 book ai didi

python - 谷歌CP : Creating Client() object locally

转载 作者:行者123 更新时间:2023-12-01 00:11:57 30 4
gpt4 key购买 nike

我有这个类,用于登录 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"))

我的整个代码都崩溃了。

问题:如果不在云端,是否有办法跳过实例化此类?比如有条件的类(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,而在本地环境中运行较旧版本。

关于python - 谷歌CP : Creating Client() object locally,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59578320/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com