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

Do not check for S3 key before attempting download #19504

Merged
merged 2 commits into from
Nov 10, 2021
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
13 changes: 9 additions & 4 deletions airflow/providers/amazon/aws/hooks/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,15 @@ def download_file(
"""
self.log.info('Downloading source S3 file from Bucket %s with path %s', bucket_name, key)

if not self.check_for_key(key, bucket_name):
raise AirflowException(f'The source file in Bucket {bucket_name} with path {key} does not exist')

s3_obj = self.get_key(key, bucket_name)
try:
s3_obj = self.get_key(key, bucket_name)
except ClientError as e:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my preference would be to not catch this error at all, but catching it keeps this consistent with the existing behavior

if e.response.get('Error', {}).get('Code') == 404:
raise AirflowException(
f'The source file in Bucket {bucket_name} with path {key} does not exist'
)
else:
raise e

with NamedTemporaryFile(dir=local_path, prefix='airflow_tmp_', delete=False) as local_tmp_file:
s3_obj.download_fileobj(local_tmp_file)
Expand Down
1 change: 0 additions & 1 deletion tests/providers/amazon/aws/hooks/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ def test_download_file(self, mock_temp_file):

s3_hook.download_file(key=key, bucket_name=bucket)

s3_hook.check_for_key.assert_called_once_with(key, bucket)
s3_hook.get_key.assert_called_once_with(key, bucket)
s3_obj.download_fileobj.assert_called_once_with(mock_temp_file)

Expand Down