|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Asynchronous client for interacting with Google Cloud Storage.""" |
| 16 | + |
| 17 | +import functools |
| 18 | + |
| 19 | +from google.cloud.storage._experimental.asyncio.async_helpers import ASYNC_DEFAULT_TIMEOUT |
| 20 | +from google.cloud.storage._experimental.asyncio.async_helpers import ASYNC_DEFAULT_RETRY |
| 21 | +from google.cloud.storage._experimental.asyncio.async_helpers import AsyncHTTPIterator |
| 22 | +from google.cloud.storage._experimental.asyncio.async_helpers import _do_nothing_page_start |
| 23 | +from google.cloud.storage._opentelemetry_tracing import create_trace_span |
| 24 | +from google.cloud.storage._experimental.asyncio.async_creds import AsyncCredsWrapper |
| 25 | +from google.cloud.storage.abstracts.base_client import BaseClient |
| 26 | +from google.cloud.storage._experimental.asyncio.async_connection import AsyncConnection |
| 27 | +from google.cloud.storage.abstracts import base_client |
| 28 | + |
| 29 | +try: |
| 30 | + from google.auth.aio.transport import sessions |
| 31 | + AsyncSession = sessions.AsyncAuthorizedSession |
| 32 | + _AIO_AVAILABLE = True |
| 33 | +except ImportError: |
| 34 | + AsyncSession = object |
| 35 | + _AIO_AVAILABLE = False |
| 36 | + |
| 37 | +_marker = base_client.marker |
| 38 | + |
| 39 | + |
| 40 | +class AsyncClient(BaseClient): |
| 41 | + """Asynchronous client to interact with Google Cloud Storage.""" |
| 42 | + |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + project=_marker, |
| 46 | + credentials=None, |
| 47 | + _async_http=None, |
| 48 | + client_info=None, |
| 49 | + client_options=None, |
| 50 | + extra_headers={}, |
| 51 | + *, |
| 52 | + api_key=None, |
| 53 | + ): |
| 54 | + if not _AIO_AVAILABLE: |
| 55 | + # Python 3.9 or less comes with an older version of google-auth library which doesn't support asyncio |
| 56 | + raise ImportError( |
| 57 | + "Failed to import 'google.auth.aio', Consider using a newer python version (>=3.10)" |
| 58 | + " or newer version of google-auth library to mitigate this issue." |
| 59 | + ) |
| 60 | + |
| 61 | + if self._use_client_cert: |
| 62 | + # google.auth.aio.transports.sessions.AsyncAuthorizedSession currently doesn't support configuring mTLS. |
| 63 | + # In future, we can monkey patch the above, and do provide mTLS support, but that is not a priority |
| 64 | + # at the moment. |
| 65 | + raise ValueError("Async Client currently do not support mTLS") |
| 66 | + |
| 67 | + # We initialize everything as per synchronous client. |
| 68 | + super().__init__( |
| 69 | + project=project, |
| 70 | + credentials=credentials, |
| 71 | + client_info=client_info, |
| 72 | + client_options=client_options, |
| 73 | + extra_headers=extra_headers, |
| 74 | + api_key=api_key |
| 75 | + ) |
| 76 | + self.credentials = AsyncCredsWrapper(self._credentials) # self._credential is synchronous. |
| 77 | + self._connection = AsyncConnection(self, **self.connection_kw_args) # adapter for async communication |
| 78 | + self._async_http_internal = _async_http |
| 79 | + self._async_http_passed_by_user = (_async_http is not None) |
| 80 | + |
| 81 | + @property |
| 82 | + def async_http(self): |
| 83 | + """Returns the existing asynchronous session, or create one if it does not exists.""" |
| 84 | + if self._async_http_internal is None: |
| 85 | + self._async_http_internal = AsyncSession(credentials=self.credentials) |
| 86 | + return self._async_http_internal |
| 87 | + |
| 88 | + async def close(self): |
| 89 | + """Close the session, if it exists""" |
| 90 | + if self._async_http_internal is not None and not self._async_http_passed_by_user: |
| 91 | + await self._async_http_internal.close() |
| 92 | + |
| 93 | + async def _get_resource( |
| 94 | + self, |
| 95 | + path, |
| 96 | + query_params=None, |
| 97 | + headers=None, |
| 98 | + timeout=ASYNC_DEFAULT_TIMEOUT, |
| 99 | + retry=ASYNC_DEFAULT_RETRY, |
| 100 | + _target_object=None, |
| 101 | + ): |
| 102 | + """See super() class""" |
| 103 | + return await self._connection.api_request( |
| 104 | + method="GET", |
| 105 | + path=path, |
| 106 | + query_params=query_params, |
| 107 | + headers=headers, |
| 108 | + timeout=timeout, |
| 109 | + retry=retry, |
| 110 | + _target_object=_target_object, |
| 111 | + ) |
| 112 | + |
| 113 | + def _list_resource( |
| 114 | + self, |
| 115 | + path, |
| 116 | + item_to_value, |
| 117 | + page_token=None, |
| 118 | + max_results=None, |
| 119 | + extra_params=None, |
| 120 | + page_start=_do_nothing_page_start, |
| 121 | + page_size=None, |
| 122 | + timeout=ASYNC_DEFAULT_TIMEOUT, |
| 123 | + retry=ASYNC_DEFAULT_RETRY, |
| 124 | + ): |
| 125 | + """See super() class""" |
| 126 | + kwargs = { |
| 127 | + "method": "GET", |
| 128 | + "path": path, |
| 129 | + "timeout": timeout, |
| 130 | + } |
| 131 | + with create_trace_span( |
| 132 | + name="Storage.AsyncClient._list_resource_returns_iterator", |
| 133 | + client=self, |
| 134 | + api_request=kwargs, |
| 135 | + retry=retry, |
| 136 | + ): |
| 137 | + api_request = functools.partial( |
| 138 | + self._connection.api_request, timeout=timeout, retry=retry |
| 139 | + ) |
| 140 | + return AsyncHTTPIterator( |
| 141 | + client=self, |
| 142 | + api_request=api_request, |
| 143 | + path=path, |
| 144 | + item_to_value=item_to_value, |
| 145 | + page_token=page_token, |
| 146 | + max_results=max_results, |
| 147 | + extra_params=extra_params, |
| 148 | + page_start=page_start, |
| 149 | + page_size=page_size, |
| 150 | + ) |
| 151 | + |
| 152 | + async def _patch_resource( |
| 153 | + self, |
| 154 | + path, |
| 155 | + data, |
| 156 | + query_params=None, |
| 157 | + headers=None, |
| 158 | + timeout=ASYNC_DEFAULT_TIMEOUT, |
| 159 | + retry=None, |
| 160 | + _target_object=None, |
| 161 | + ): |
| 162 | + """See super() class""" |
| 163 | + return await self._connection.api_request( |
| 164 | + method="PATCH", |
| 165 | + path=path, |
| 166 | + data=data, |
| 167 | + query_params=query_params, |
| 168 | + headers=headers, |
| 169 | + timeout=timeout, |
| 170 | + retry=retry, |
| 171 | + _target_object=_target_object, |
| 172 | + ) |
| 173 | + |
| 174 | + async def _put_resource( |
| 175 | + self, |
| 176 | + path, |
| 177 | + data, |
| 178 | + query_params=None, |
| 179 | + headers=None, |
| 180 | + timeout=ASYNC_DEFAULT_TIMEOUT, |
| 181 | + retry=None, |
| 182 | + _target_object=None, |
| 183 | + ): |
| 184 | + """See super() class""" |
| 185 | + return await self._connection.api_request( |
| 186 | + method="PUT", |
| 187 | + path=path, |
| 188 | + data=data, |
| 189 | + query_params=query_params, |
| 190 | + headers=headers, |
| 191 | + timeout=timeout, |
| 192 | + retry=retry, |
| 193 | + _target_object=_target_object, |
| 194 | + ) |
| 195 | + |
| 196 | + async def _post_resource( |
| 197 | + self, |
| 198 | + path, |
| 199 | + data, |
| 200 | + query_params=None, |
| 201 | + headers=None, |
| 202 | + timeout=ASYNC_DEFAULT_TIMEOUT, |
| 203 | + retry=None, |
| 204 | + _target_object=None, |
| 205 | + ): |
| 206 | + """See super() class""" |
| 207 | + return await self._connection.api_request( |
| 208 | + method="POST", |
| 209 | + path=path, |
| 210 | + data=data, |
| 211 | + query_params=query_params, |
| 212 | + headers=headers, |
| 213 | + timeout=timeout, |
| 214 | + retry=retry, |
| 215 | + _target_object=_target_object, |
| 216 | + ) |
| 217 | + |
| 218 | + async def _delete_resource( |
| 219 | + self, |
| 220 | + path, |
| 221 | + query_params=None, |
| 222 | + headers=None, |
| 223 | + timeout=ASYNC_DEFAULT_TIMEOUT, |
| 224 | + retry=ASYNC_DEFAULT_RETRY, |
| 225 | + _target_object=None, |
| 226 | + ): |
| 227 | + """See super() class""" |
| 228 | + return await self._connection.api_request( |
| 229 | + method="DELETE", |
| 230 | + path=path, |
| 231 | + query_params=query_params, |
| 232 | + headers=headers, |
| 233 | + timeout=timeout, |
| 234 | + retry=retry, |
| 235 | + _target_object=_target_object, |
| 236 | + ) |
| 237 | + |
| 238 | + def bucket(self, bucket_name, user_project=None, generation=None): |
| 239 | + """See super() class""" |
| 240 | + raise NotImplementedError("This bucket class needs to be implemented.") |
0 commit comments