|
3 | 3 | import os |
4 | 4 | from pathlib import Path, PurePosixPath |
5 | 5 | from typing import Any, Callable, Dict, Iterable, Optional, TYPE_CHECKING, Tuple, Union |
| 6 | +import warnings |
6 | 7 |
|
7 | 8 | from ..client import Client, register_client_class |
8 | 9 | from ..cloudpath import implementation_registry |
|
16 | 17 | from google.api_core.exceptions import NotFound |
17 | 18 | from google.auth.exceptions import DefaultCredentialsError |
18 | 19 | from google.cloud.storage import Client as StorageClient |
19 | | - from google.cloud.storage import transfer_manager |
20 | | - |
21 | 20 |
|
22 | 21 | except ModuleNotFoundError: |
23 | 22 | implementation_registry["gs"].dependencies_loaded = False |
24 | 23 |
|
25 | 24 |
|
| 25 | +try: |
| 26 | + from google.cloud.storage import transfer_manager |
| 27 | +except ImportError: |
| 28 | + transfer_manager = None |
| 29 | + |
| 30 | + |
26 | 31 | @register_client_class("gs") |
27 | 32 | class GSClient(Client): |
28 | 33 | """Client class for Google Cloud Storage which handles authentication with GCP for |
@@ -80,7 +85,7 @@ def __init__( |
80 | 85 | writing a file to the cloud. Defaults to `mimetypes.guess_type`. Must return a tuple (content type, content encoding). |
81 | 86 | download_chunks_concurrently_kwargs (Optional[Dict[str, Any]]): Keyword arguments to pass to |
82 | 87 | [`download_chunks_concurrently`](https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.transfer_manager#google_cloud_storage_transfer_manager_download_chunks_concurrently) |
83 | | - for sliced parallel downloads. |
| 88 | + for sliced parallel downloads; Only available in `google-cloud-storage` version 2.7.0 or later, otherwise ignored and a warning is emitted. |
84 | 89 | """ |
85 | 90 | if application_credentials is None: |
86 | 91 | application_credentials = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") |
@@ -125,11 +130,16 @@ def _download_file(self, cloud_path: GSPath, local_path: Union[str, os.PathLike] |
125 | 130 |
|
126 | 131 | local_path = Path(local_path) |
127 | 132 |
|
128 | | - if self.download_chunks_concurrently_kwargs is not None: |
| 133 | + if transfer_manager is not None and self.download_chunks_concurrently_kwargs is not None: |
129 | 134 | transfer_manager.download_chunks_concurrently( |
130 | 135 | blob, local_path, **self.download_chunks_concurrently_kwargs |
131 | 136 | ) |
132 | 137 | else: |
| 138 | + if transfer_manager is None and self.download_chunks_concurrently_kwargs is not None: |
| 139 | + warnings.warn( |
| 140 | + "Ignoring `download_chunks_concurrently_kwargs` for version of google-cloud-storage that does not support them (<2.7.0)." |
| 141 | + ) |
| 142 | + |
133 | 143 | blob.download_to_filename(local_path) |
134 | 144 |
|
135 | 145 | return local_path |
|
0 commit comments