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

huangapple go评论89阅读模式
英文:

GCP: Creating Client() object locally

问题

以下是翻译好的部分:

我有这个用于日志记录到 StackDriver 的类

```python
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")) 处中断,并且我的整个代码都会崩溃。

问题: 是否有办法在不在云上运行时跳过实例化这个类?比如条件类?

或者

问题: 是否有办法让这个在本地运行?它应该可以运行。我已经提供了凭据和项目,根据StackDriver文档,如果我提供了项目和凭据,它应该可以在本地和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

<details>
<summary>英文:</summary>

I have this class which is used for logging to 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(&quot;/path/to/creds&quot;))

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

When run on the google cloud, this works seamlessly. However, when run locally it breaks at `CREDS = google.cloud.logging.Client(project=PROJECT, credentials=service_account.Credentials.from_service_account_file(&quot;/path/to/creds&quot;))` 

And my entire code breaks. 

**Question:** Is there any way to skip instantiating this class if not on the cloud? Like a conditional class? 

*OR*

**Question:** Is there any way to make this work locally? It should work. I give it the creds and the project and according to [StackDriver docs](https://cloud.google.com/logging/docs/setup/go) if I give it the project + creds, it should work locally as well as in GCP?


When it breaks this is the traceback:

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


</details>


# 答案1
**得分**: 2

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

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

<details>
<summary>英文:</summary>

Google added the `client_info` parameter in this change: https://github.com/googleapis/google-cloud-python/pull/7849/files#diff-340196d499e9d0eea25cd457f53bfa42L31

I suspect you are running a newer version of the Google Cloud Python SDK in your GCP environment and and older version on your local environment.

</details>



huangapple
  • 本文由 发表于 2020年1月3日 20:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定