You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/10/06 02:34:24 UTC

[GitHub] [airflow] uranusjr commented on a diff in pull request #26886: Adding `preserve_file_name` param to `S3Hook.download_file` method

uranusjr commented on code in PR #26886:
URL: https://github.com/apache/airflow/pull/26886#discussion_r988498371


##########
airflow/providers/amazon/aws/hooks/s3.py:
##########
@@ -909,7 +919,17 @@ def download_file(self, key: str, bucket_name: str | None = None, local_path: st
                 Config=self.transfer_config,
             )
 
-        return local_tmp_file.name
+        if preserve_file_name:
+            filename_in_s3 = s3_obj.key.split('/')[-1]
+            local_folder_name = local_tmp_file.name.rsplit('/', 1)[0]
+            local_file_name = f"{local_folder_name}/{filename_in_s3}"
+
+            self.log.info("Renaming file from %s to %s", local_tmp_file.name, local_file_name)
+            rename(local_tmp_file.name, local_file_name)
+
+            return local_file_name

Review Comment:
   ```suggestion
               filename_in_s3 = s3_obj.key.rsplit('/', 1)[-1]
               local_tmp_file_name = Path(local_tmp_file.name)
               local_file_name = local_tmp_file_name.with_name(filename_in_s3)
   
               self.log.info("Renaming file from %s to %s", local_tmp_file_name, local_file_name)
               local_tmp_file_name.rename(local_file_name)
   
               return str(local_file_name)
   ```
   
   Don’t do string manipulation on the path, use structured calls instead.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org