diff --git a/airflow/providers/amazon/aws/hooks/s3.py b/airflow/providers/amazon/aws/hooks/s3.py index 4a2a15b7a9393..53b2747104380 100644 --- a/airflow/providers/amazon/aws/hooks/s3.py +++ b/airflow/providers/amazon/aws/hooks/s3.py @@ -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: + 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) diff --git a/tests/providers/amazon/aws/hooks/test_s3.py b/tests/providers/amazon/aws/hooks/test_s3.py index d4d20b530da1b..934993c040566 100644 --- a/tests/providers/amazon/aws/hooks/test_s3.py +++ b/tests/providers/amazon/aws/hooks/test_s3.py @@ -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)