Skip to content

use blob.exists to check the GCS file #34818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions sdks/python/apache_beam/io/gcp/gcsio.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,10 @@ def exists(self, path):
Args:
path: GCS file path pattern in the form gs://<bucket>/<name>.
"""
try:
self._gcs_object(path)
return True
except NotFound:
return False
bucket_name, blob_name = parse_gcs_path(path)
bucket = self.client.bucket(bucket_name)
blob = bucket.blob(blob_name)
return blob.exists(retry=self._storage_client_retry)

def checksum(self, path):
"""Looks up the checksum of a GCS object.
Expand Down
5 changes: 4 additions & 1 deletion sdks/python/apache_beam/io/gcp/gcsio_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ def download_as_bytes(self, **kwargs):
def __eq__(self, other):
return self.bucket.get_blob(self.name) is other.bucket.get_blob(other.name)

def exists(self, **kwargs):
return self.bucket.get_blob(self.name) is not None


@unittest.skipIf(NotFound is None, 'GCP dependencies are not installed')
class TestGCSPathParser(unittest.TestCase):
Expand Down Expand Up @@ -366,7 +369,7 @@ def test_exists(self):
self.assertFalse(self.gcs.exists(file_name + 'xyz'))
self.assertTrue(self.gcs.exists(file_name))

@mock.patch.object(FakeBucket, 'get_blob')
@mock.patch.object(FakeBlob, 'exists')
def test_exists_failure(self, mock_get):
# Raising an error other than 404. Raising 404 is a valid failure for
# exists() call.
Expand Down
Loading