diff --git a/google/cloud/storage/client.py b/google/cloud/storage/client.py index 42358ef68..51fc8ec73 100644 --- a/google/cloud/storage/client.py +++ b/google/cloud/storage/client.py @@ -20,6 +20,7 @@ import datetime import functools import json +import os import warnings import google.api_core.client_options @@ -33,6 +34,7 @@ from google.cloud.exceptions import NotFound from google.cloud.storage._helpers import _get_storage_host from google.cloud.storage._helpers import _bucket_bound_hostname_url +from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR from google.cloud.storage._http import Connection from google.cloud.storage._signing import ( get_expiration_seconds_v4, @@ -119,6 +121,16 @@ def __init__( if project is _marker: project = None + if os.getenv(STORAGE_EMULATOR_ENV_VAR) is not None: + credentials = AnonymousCredentials() + if project in [_marker, None]: + project = os.getenv("GCLOUD_PROJECT") + if project is None: + raise ValueError( + "To use the GCS emulator, pass 'project' as an argument " + "or set GCLOUD_PROJECT environment variable" + ) + super(Client, self).__init__( project=project, credentials=credentials, diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index ee0e387dd..da9d1ddae 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -219,6 +219,53 @@ def test_ctor_w_client_info(self): self.assertEqual(list(client._batch_stack), []) self.assertIs(client._connection._client_info, client_info) + def test_ctor_w_emulator_w_env_var_glcoud_project(self): + from google.auth.credentials import AnonymousCredentials + from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR + + HOST = "https://api.example.com" + PROJECT = "test" + environ = { + STORAGE_EMULATOR_ENV_VAR: HOST, + "GCLOUD_PROJECT": PROJECT, + } + + with mock.patch("os.environ", environ): + client = self._make_one() + + self.assertEqual(client.project, PROJECT) + self.assertEqual(client._connection.API_BASE_URL, HOST) + self.assertIsInstance(client._credentials, AnonymousCredentials) + + def test_ctor_w_emulator_w_project_argument(self): + from google.auth.credentials import AnonymousCredentials + from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR + + HOST = "https://api.example.com" + PROJECT = "test" + ENVIRON_PROJECT = "environ-project" + environ = { + STORAGE_EMULATOR_ENV_VAR: HOST, + "GCLOUD_PROJECT": ENVIRON_PROJECT, + } + + with mock.patch("os.environ", environ): + client = self._make_one(project=PROJECT) + + self.assertEqual(client.project, PROJECT) + self.assertEqual(client._connection.API_BASE_URL, HOST) + self.assertIsInstance(client._credentials, AnonymousCredentials) + + def test_ctor_w_emulator_missing_project(self): + from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR + + HOST = "https://api.example.com" + environ = {STORAGE_EMULATOR_ENV_VAR: HOST} + + with mock.patch("os.environ", environ): + with self.assertRaises(ValueError): + self._make_one() + def test_create_anonymous_client(self): from google.auth.credentials import AnonymousCredentials from google.cloud.storage._http import Connection