|
15 | 15 | from google.auth.credentials import Credentials
|
16 | 16 | from google.api_core.retry import Retry
|
17 | 17 |
|
| 18 | + from google.auth import default as google_default_auth |
18 | 19 | from google.auth.exceptions import DefaultCredentialsError
|
19 | 20 | from google.cloud.storage import Client as StorageClient
|
20 | 21 |
|
@@ -51,18 +52,14 @@ def __init__(
|
51 | 52 | ):
|
52 | 53 | """Class constructor. Sets up a [`Storage
|
53 | 54 | Client`](https://googleapis.dev/python/storage/latest/client.html).
|
54 |
| - Supports the following authentication methods of `Storage Client`. |
| 55 | + Supports, in this order, the following authentication methods of `Storage Client`. |
55 | 56 |
|
56 |
| - - Environment variable `"GOOGLE_APPLICATION_CREDENTIALS"` containing a |
57 |
| - path to a JSON credentials file for a Google service account. See |
58 |
| - [Authenticating as a Service |
59 |
| - Account](https://cloud.google.com/docs/authentication/production). |
60 |
| - - File path to a JSON credentials file for a Google service account. |
61 |
| - - OAuth2 Credentials object and a project name. |
62 | 57 | - Instantiated and already authenticated `Storage Client`.
|
| 58 | + - OAuth2 Credentials object and a project name. |
| 59 | + - File path to a JSON credentials file for a Google service account. |
| 60 | + - Google Cloud SDK default credentials. See [How Application Default Credentials works](https://cloud.google.com/docs/authentication/application-default-credentials) |
63 | 61 |
|
64 |
| - If multiple methods are used, priority order is reverse of list above |
65 |
| - (later in list takes priority). If no authentication methods are used, |
| 62 | + If no authentication methods are used, |
66 | 63 | then the client will be instantiated as anonymous, which will only have
|
67 | 64 | access to public buckets.
|
68 | 65 |
|
@@ -91,18 +88,24 @@ def __init__(
|
91 | 88 | timeout (Optional[float]): Cloud Storage [timeout value](https://cloud.google.com/python/docs/reference/storage/1.39.0/retry_timeout)
|
92 | 89 | retry (Optional[google.api_core.retry.Retry]): Cloud Storage [retry configuration](https://cloud.google.com/python/docs/reference/storage/1.39.0/retry_timeout#configuring-retries)
|
93 | 90 | """
|
94 |
| - if application_credentials is None: |
95 |
| - application_credentials = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") |
96 |
| - |
| 91 | + # don't check `GOOGLE_APPLICATION_CREDENTIALS` since `google_default_auth` already does that |
| 92 | + # use explicit client |
97 | 93 | if storage_client is not None:
|
98 | 94 | self.client = storage_client
|
| 95 | + # use explicit credentials |
99 | 96 | elif credentials is not None:
|
100 | 97 | self.client = StorageClient(credentials=credentials, project=project)
|
| 98 | + # use explicit credential file |
101 | 99 | elif application_credentials is not None:
|
102 | 100 | self.client = StorageClient.from_service_account_json(application_credentials)
|
| 101 | + # use default credentials based on SDK precedence |
103 | 102 | else:
|
104 | 103 | try:
|
105 |
| - self.client = StorageClient() |
| 104 | + # use `google_default_auth` instead of `StorageClient()` since it |
| 105 | + # handles precedence of creds in different locations properly |
| 106 | + credentials, default_project = google_default_auth() |
| 107 | + project = project or default_project # use explicit project if present |
| 108 | + self.client = StorageClient(credentials=credentials, project=project) |
106 | 109 | except DefaultCredentialsError:
|
107 | 110 | self.client = StorageClient.create_anonymous_client()
|
108 | 111 |
|
|
0 commit comments