gpt4 book ai didi

python - Airflow:如何将 PDF 文件从 S3 存储桶下载到 Airflow

转载 作者:行者123 更新时间:2023-12-02 19:07:05 28 4
gpt4 key购买 nike

尝试了下面的代码,但收到错误“无法找到凭据”def 下载():

bucket = 'bucketname'
key = 'path and filename'
s3_resource = boto3.resource('s3')
my_bucket = s3_resource.Bucket(bucket)
objects = my_bucket.objects.filter(Prefix=key)
for obj = objects:
path,filename = os.path.split(obj.key)
my_bucket.download_file(obj.key, filename)

最佳答案

您需要定义 AWS 连接并使用通过 S3Hook 实现 download_fileobj 函数。

我没有测试它,但它应该是这样的:

from tempfile import NamedTemporaryFile
from airflow.providers.amazon.aws.hooks.s3 import S3Hook
hook = S3Hook('my_aws_conn')
key_object = hook.get_key('your_path')
with NamedTemporaryFile("wb") as f:
key_object.download_fileobj(Fileobj=f)
f.flush()

关于python - Airflow:如何将 PDF 文件从 S3 存储桶下载到 Airflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64873226/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com