我正在尝试使用气流中的 S3Hook 从 S3 上的存储桶位置下载文件。
下面是我的代码
def s3_extract(key: str, bucket_name: str, local_path: str) -> str:
source_s3_key = key
source_s3_bucket = bucket_name
dest_file_path = local_path
source_s3 = S3Hook(aws_conn_id = "aws_conn_str")#This is my connection defined for S3Hooks
source_s3.download_file(source_s3_key, source_s3_bucket,f"{dest_file_path}/filename.txt")
file = open(f"{dest_file_path}/filename.txt","r")
text = file.read()
file.close()
return text
download_job = PythonOperator(
task_id = "s3_download_task",
python_callable = s3_extract,
op_kwargs = {
'key':'airflow/docs/filename.txt',
'bucket_name':'s3-dev-data-001',
'local_path':'usr/local/airflow'
}
)
当我尝试运行上述代码时,出现错误
FileNotFoundError: [Errno 2] no such file or directory: 'usr/local/airflow/filename.txt/**airflow_tmp_90_6ogw5**'
为什么粗体被添加到目标路径的末尾,我该怎么做才能删除它?
谢谢
您可以使用两个额外的参数来下载函数,以使用目标目录作为 tmp 目录并保留与源相同的文件名
preserve_file_name=True, use_autogenerated_subdir=False
更多详细信息请参见文档