Skip to content

Commit 1f31eb0

Browse files
authored
use blob.exists to check the GCS file (#34818)
* use blob.exists to check the GCS file * added try * fixed the tests
1 parent d427d37 commit 1f31eb0

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

sdks/python/apache_beam/io/gcp/gcsio.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,10 @@ def exists(self, path):
481481
Args:
482482
path: GCS file path pattern in the form gs://<bucket>/<name>.
483483
"""
484-
try:
485-
self._gcs_object(path)
486-
return True
487-
except NotFound:
488-
return False
484+
bucket_name, blob_name = parse_gcs_path(path)
485+
bucket = self.client.bucket(bucket_name)
486+
blob = bucket.blob(blob_name)
487+
return blob.exists(retry=self._storage_client_retry)
489488

490489
def checksum(self, path):
491490
"""Looks up the checksum of a GCS object.

sdks/python/apache_beam/io/gcp/gcsio_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ def download_as_bytes(self, **kwargs):
231231
def __eq__(self, other):
232232
return self.bucket.get_blob(self.name) is other.bucket.get_blob(other.name)
233233

234+
def exists(self, **kwargs):
235+
return self.bucket.get_blob(self.name) is not None
236+
234237

235238
@unittest.skipIf(NotFound is None, 'GCP dependencies are not installed')
236239
class TestGCSPathParser(unittest.TestCase):
@@ -396,7 +399,7 @@ def test_exists(self):
396399
self.assertFalse(self.gcs.exists(file_name + 'xyz'))
397400
self.assertTrue(self.gcs.exists(file_name))
398401

399-
@mock.patch.object(FakeBucket, 'get_blob')
402+
@mock.patch.object(FakeBlob, 'exists')
400403
def test_exists_failure(self, mock_get):
401404
# Raising an error other than 404. Raising 404 is a valid failure for
402405
# exists() call.

0 commit comments

Comments
 (0)