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

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

GCP: Creating Client() object locally

问题

以下是翻译好的部分:

  1. 我有这个用于日志记录到 StackDriver 的类
  2. ```python
  3. from google.cloud.logging.handlers import CloudLoggingHandler
  4. from google.oauth2 import service_account
  5. class GoogleLogger(CloudLoggingHandler):
  6. CREDS = google.cloud.logging.Client(
  7. project=PROJECT, credentials=service_account.Credentials.from_service_account_file("/path/to/creds"))
  8. def __init__(self, client=CREDS):
  9. 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上运行?

当出现问题时,以下是回溯信息:

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

  1. CREDS = google.cloud.logging.Client(
  2. project=PROJECT, credentials=service_account.Credentials.from_service_account_file(&quot;/path/to/creds&quot;))
  3. def __init__(self, client=CREDS):
  4. super(GoogleLogger, self).__init__(client)
  1. 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;))`
  2. And my entire code breaks.
  3. **Question:** Is there any way to skip instantiating this class if not on the cloud? Like a conditional class?
  4. *OR*
  5. **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?
  6. 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

  1. </details>
  2. # 答案1
  3. **得分**: 2
  4. Google在此更改中添加了`client_info`参数:https://github.com/googleapis/google-cloud-python/pull/7849/files#diff-340196d499e9d0eea25cd457f53bfa42L31
  5. 我怀疑您在GCP环境中运行的是Google Cloud Python SDK的更新版本,在本地环境中运行的是较旧的版本。
  6. <details>
  7. <summary>英文:</summary>
  8. Google added the `client_info` parameter in this change: https://github.com/googleapis/google-cloud-python/pull/7849/files#diff-340196d499e9d0eea25cd457f53bfa42L31
  9. 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.
  10. </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:

确定