Skip to content
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

Fix exists method to support using Requester Pays #46759

Merged
merged 3 commits into from
Feb 23, 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
18 changes: 13 additions & 5 deletions providers/google/src/airflow/providers/google/cloud/hooks/gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,17 +598,25 @@ def _call_with_retry(f: Callable[[], None]) -> None:
context=self, scheme="gs", asset_kwargs={"bucket": bucket.name, "key": blob.name}
)

def exists(self, bucket_name: str, object_name: str, retry: Retry = DEFAULT_RETRY) -> bool:
def exists(
self,
bucket_name: str,
object_name: str,
retry: Retry = DEFAULT_RETRY,
user_project: str | None = None,
) -> bool:
"""
Check for the existence of a file in Google Cloud Storage.

:param bucket_name: The Google Cloud Storage bucket where the object is.
:param object_name: The name of the blob_name to check in the Google cloud
storage bucket.
:param retry: (Optional) How to retry the RPC
:param user_project: The identifier of the Google Cloud project to bill for the request.
Required for Requester Pays buckets.
"""
client = self.get_conn()
bucket = client.bucket(bucket_name)
bucket = client.bucket(bucket_name, user_project=user_project)
blob = bucket.blob(blob_name=object_name)
return blob.exists(retry=retry)

Expand All @@ -625,7 +633,7 @@ def get_blob_update_time(self, bucket_name: str, object_name: str):

def is_updated_after(self, bucket_name: str, object_name: str, ts: datetime) -> bool:
"""
Check if an blob_name is updated in Google Cloud Storage.
Check if a blob_name is updated in Google Cloud Storage.

:param bucket_name: The Google Cloud Storage bucket where the object is.
:param object_name: The name of the object to check in the Google cloud
Expand All @@ -645,7 +653,7 @@ def is_updated_between(
self, bucket_name: str, object_name: str, min_ts: datetime, max_ts: datetime
) -> bool:
"""
Check if an blob_name is updated in Google Cloud Storage.
Check if a blob_name is updated in Google Cloud Storage.

:param bucket_name: The Google Cloud Storage bucket where the object is.
:param object_name: The name of the object to check in the Google cloud
Expand All @@ -666,7 +674,7 @@ def is_updated_between(

def is_updated_before(self, bucket_name: str, object_name: str, ts: datetime) -> bool:
"""
Check if an blob_name is updated before given time in Google Cloud Storage.
Check if a blob_name is updated before given time in Google Cloud Storage.

:param bucket_name: The Google Cloud Storage bucket where the object is.
:param object_name: The name of the object to check in the Google cloud
Expand Down
6 changes: 3 additions & 3 deletions providers/google/tests/unit/google/cloud/hooks/test_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ def test_exists(self, mock_service):
exists_method.return_value = True

# When
response = self.gcs_hook.exists(bucket_name=test_bucket, object_name=test_object)
response = self.gcs_hook.exists(bucket_name=test_bucket, object_name=test_object, user_project=None)

# Then
assert response
bucket_mock.assert_called_once_with(test_bucket)
bucket_mock.assert_called_once_with(test_bucket, user_project=None)
blob_object.assert_called_once_with(blob_name=test_object)
exists_method.assert_called_once_with(retry=DEFAULT_RETRY)

Expand All @@ -226,7 +226,7 @@ def test_exists_nonexisting_object(self, mock_service):
exists_method.return_value = False

# When
response = self.gcs_hook.exists(bucket_name=test_bucket, object_name=test_object)
response = self.gcs_hook.exists(bucket_name=test_bucket, object_name=test_object, user_project=None)

# Then
assert not response
Expand Down
Loading