Skip to content

Commit

Permalink
fix: use native exists() method in GSClient (#420) (#421)
Browse files Browse the repository at this point in the history
* fix: use native `exists()` method in `GSClient`

* update HISTORY.md

* only short-circuit

* fix lint

* update tests

* lint and tests

Co-authored-by: Aaron Bach <bachya1208@gmail.com>
  • Loading branch information
pjbull and bachya authored Apr 5, 2024
1 parent c9da3f3 commit d368501
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix `CloudPath` cleanup via `CloudPath.__del__` when `Client` encounters an exception during initialization and does not create a `file_cache_mode` attribute. (Issue [#372](https://github.com/drivendataorg/cloudpathlib/issues/372), thanks to [@bryanwweber](https://github.com/bryanwweber))
- Drop support for Python 3.7; pin minimal `boto3` version to Python 3.8+ versions. (PR [#407](https://github.com/drivendataorg/cloudpathlib/pull/407))
- fix: use native `exists()` method in `GSClient`. (PR [#420](https://github.com/drivendataorg/cloudpathlib/pull/420))

## v0.18.1 (2024-02-26)

Expand Down
7 changes: 1 addition & 6 deletions cloudpathlib/gs/gsclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
if TYPE_CHECKING:
from google.auth.credentials import Credentials

from google.api_core.exceptions import NotFound
from google.auth.exceptions import DefaultCredentialsError
from google.cloud.storage import Client as StorageClient

Expand Down Expand Up @@ -171,11 +170,7 @@ def _is_file_or_dir(self, cloud_path: GSPath) -> Optional[str]:
def _exists(self, cloud_path: GSPath) -> bool:
# short-circuit the root-level bucket
if not cloud_path.blob:
try:
next(self.client.bucket(cloud_path.bucket).list_blobs())
return True
except NotFound:
return False
return self.client.bucket(cloud_path.bucket).exists()

return self._is_file_or_dir(cloud_path) in ["file", "dir"]

Expand Down
6 changes: 6 additions & 0 deletions tests/mock_clients/mock_gs.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ def copy_blob(self, blob, destination_bucket, new_name):
dst.parent.mkdir(exist_ok=True, parents=True)
dst.write_bytes(data)

def exists(self):
if self.bucket_name == DEFAULT_GS_BUCKET_NAME: # name used by passing tests
return True
else:
return False

def get_blob(self, blob):
if (self.name / blob).is_file():
return MockBlob(self.name, blob, client=self.client)
Expand Down

0 comments on commit d368501

Please sign in to comment.