禁用模拟 google.cloud.storage 时的 Google 云身份验证 Python。

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

Disable google cloud authentication python when mocking google.cloud.storage

问题

以下是您要翻译的部分:

  1. 从以下的Google Cloud Storage模拟开始
  2. ```python
  3. from google.cloud import storage
  4. class MockBlob:
  5. def download_as_string(self) -> bytes:
  6. return bytes("\n".join(INPUT_IDS), "utf-8")
  7. class MockBucket:
  8. def get_blob(self, path: str) -> MockBlob:
  9. return MockBlob()
  10. class MockStorageClient(storage.Client):
  11. def __init__(self, *args, **kwargs):
  12. super().__init__(*args, **kwargs)
  13. def _require_client_info(self, client_info=None):
  14. pass
  15. def _require_virtual(self):
  16. pass
  17. def get_bucket(self, bucket_or_name: str):
  18. return MockBucket()

我收到以下错误消息

  1. .venv/lib/python3.8/site-packages/google/cloud/storage/client.py:173: in __init__
  2. super(Client, self).__init__(
  3. .venv/lib/python3.8/site-packages/google/cloud/client/__init__.py:320: in __init__
  4. _ClientProjectMixin.__init__(self, project=project, credentials=credentials)
  5. .venv/lib/python3.8/site-packages/google/cloud/client/__init__.py:268: in __init__
  6. project = self._determine_default(project)
  7. .venv/lib/python3.8/site-packages/google/cloud/client/__init__.py:287: in _determine_default
  8. return _determine_default_project(project)
  9. .venv/lib/python3.8/site-packages/google/cloud/_helpers/__init__.py:152: in _determine_default_project
  10. _, project = google.auth.default()
  11. .venv/lib/python3.8/site-packages/google/auth/_default.py:615: in default
  12. credentials, project_id = checker()
  13. .venv/lib/python3.8/site-packages/google/auth/_default.py:608: in <lambda>
  14. lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
  15. .venv/lib/python3.8/site-packages/google/auth/_default.py:228: in _get_explicit_environ_credentials
  16. credentials, project_id = load_credentials_from_file(
  17. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  18. filename = '', scopes = None, default_scopes = None, quota_project_id = None, request = None
  19. def load_credentials_from_file(
  20. filename, scopes=None, default_scopes=None, quota_project_id=None, request=None
  21. ):
  22. """Loads Google credentials from a file.
  23. The credentials file must be a service account key, stored authorized
  24. user credentials, external account credentials, or impersonated service
  25. account credentials.
  26. Args:
  27. filename (str): The full path to the credentials file.
  28. scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
  29. specified, the credentials will automatically be scoped if
  30. necessary
  31. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  32. Google client library. Use 'scopes' for user-defined scopes.
  33. quota_project_id (Optional[str]): The project ID used for
  34. quota and billing.
  35. request (Optional[google.auth.transport.Request]): An object used to make
  36. HTTP requests. This is used to determine the associated project ID
  37. for a workload identity pool resource (external account credentials).
  38. If not specified, then it will use a
  39. google.auth.transport.requests.Request client to make requests.
  40. Returns:
  41. Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
  42. credentials and the project ID. Authorized user credentials do not
  43. have the project ID information. External account credentials project
  44. IDs may not always be determined.
  45. Raises:
  46. google.auth.exceptions.DefaultCredentialsError: if the file is in the
  47. wrong format or is missing.
  48. """
  49. if not os.path.exists(filename):
  50. > raise exceptions.DefaultCredentialsError(
  51. "File {} was not found.".format(filename)
  52. )
  53. E google.auth.exceptions.DefaultCredentialsError: File was not found.
  54. .venv/lib/python3.8/site-packages/google/auth/_default.py:116: DefaultCredentialsError

测试的目的是查看是否使用存储桶的代码是否正常工作。获取的Blob包含一个由"\n"分隔的文本文件中的ID,这就是下载函数返回静态字节字符串的原因。
如何禁用凭据以避免出现问题。

英文:

With the following mock of google cloud storage

  1. from google.cloud import storage
  2. class MockBlob:
  3. def download_as_string(self) -> bytes:
  4. return bytes("\n".join(INPUT_IDS), "utf-8")
  5. class MockBucket:
  6. def get_blob(self, path: str) -> MockBlob:
  7. return MockBlob()
  8. class MockStorageClient(storage.Client):
  9. def __init__(self, *args, **kwargs):
  10. super().__init__(*args, **kwargs)
  11. def _require_client_info(self, client_info=None):
  12. pass
  13. def _require_virtual(self):
  14. pass
  15. def get_bucket(self, bucket_or_name: str):
  16. return MockBucket()

i get the error displayed below

  1. .venv/lib/python3.8/site-packages/google/cloud/storage/client.py:173: in __init__
  2. super(Client, self).__init__(
  3. .venv/lib/python3.8/site-packages/google/cloud/client/__init__.py:320: in __init__
  4. _ClientProjectMixin.__init__(self, project=project, credentials=credentials)
  5. .venv/lib/python3.8/site-packages/google/cloud/client/__init__.py:268: in __init__
  6. project = self._determine_default(project)
  7. .venv/lib/python3.8/site-packages/google/cloud/client/__init__.py:287: in _determine_default
  8. return _determine_default_project(project)
  9. .venv/lib/python3.8/site-packages/google/cloud/_helpers/__init__.py:152: in _determine_default_project
  10. _, project = google.auth.default()
  11. .venv/lib/python3.8/site-packages/google/auth/_default.py:615: in default
  12. credentials, project_id = checker()
  13. .venv/lib/python3.8/site-packages/google/auth/_default.py:608: in <lambda>
  14. lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
  15. .venv/lib/python3.8/site-packages/google/auth/_default.py:228: in _get_explicit_environ_credentials
  16. credentials, project_id = load_credentials_from_file(
  17. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  18. filename = '', scopes = None, default_scopes = None, quota_project_id = None, request = None
  19. def load_credentials_from_file(
  20. filename, scopes=None, default_scopes=None, quota_project_id=None, request=None
  21. ):
  22. """Loads Google credentials from a file.
  23. The credentials file must be a service account key, stored authorized
  24. user credentials, external account credentials, or impersonated service
  25. account credentials.
  26. Args:
  27. filename (str): The full path to the credentials file.
  28. scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
  29. specified, the credentials will automatically be scoped if
  30. necessary
  31. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  32. Google client library. Use 'scopes' for user-defined scopes.
  33. quota_project_id (Optional[str]): The project ID used for
  34. quota and billing.
  35. request (Optional[google.auth.transport.Request]): An object used to make
  36. HTTP requests. This is used to determine the associated project ID
  37. for a workload identity pool resource (external account credentials).
  38. If not specified, then it will use a
  39. google.auth.transport.requests.Request client to make requests.
  40. Returns:
  41. Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
  42. credentials and the project ID. Authorized user credentials do not
  43. have the project ID information. External account credentials project
  44. IDs may not always be determined.
  45. Raises:
  46. google.auth.exceptions.DefaultCredentialsError: if the file is in the
  47. wrong format or is missing.
  48. """
  49. if not os.path.exists(filename):
  50. > raise exceptions.DefaultCredentialsError(
  51. "File {} was not found.".format(filename)
  52. )
  53. E google.auth.exceptions.DefaultCredentialsError: File was not found.
  54. .venv/lib/python3.8/site-packages/google/auth/_default.py:116: DefaultCredentialsError

The purpose of the test is to see if the code utilizing the bucket is working. The blob that is fetched contains a text file of ids that are separated by "\n", which is why the download function returns a static byte string.
How do i disable the credentials, such that it is not an issue.

答案1

得分: 1

以下是翻译好的部分:

  1. 您可以简单地覆盖失败的函数如下所示
  2. ```python
  3. from google.auth import credentials
  4. import google.auth._default
  5. def load_credentials_from_file(
  6. filename, scopes=None, default_scopes=None, quota_project_id=None, request=None
  7. ) -> Tuple[credentials.Credentials, Optional[str]]:
  8. return (credentials.AnonymousCredentials(), "test-project")
  9. google.auth._default.load_credentials_from_file = load_credentials_from_file
  1. <details>
  2. <summary>英文:</summary>
  3. You can simply override the function that fails as
  4. ```python
  5. from google.auth import credentials
  6. import google.auth._default
  7. def load_credentials_from_file(
  8. filename, scopes=None, default_scopes=None, quota_project_id=None, request=None
  9. ) -&gt; Tuple[credentials.Credentials, Optional[str]]:
  10. return (credentials.AnonymousCredentials(), &quot;test-project&quot;)
  11. google.auth._default.load_credentials_from_file = load_credentials_from_file

huangapple
  • 本文由 发表于 2023年5月24日 18:55:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322753.html
匿名

发表评论

匿名网友

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

确定