我想下载s3文件夹(2021-02-15)中存在的所有csv文件。我尝试了以下操作,但失败了。我该怎么做?
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket')
key = 'product/myproject/2021-02-15/'
objs = list(bucket.objects.filter(Prefix=key))
for obj in objs:
client = boto3.client('s3')
client.download_file(bucket, obj, obj)
valueError: Filename must be a string
Marcin的答案是正确的,但是在不同路径下具有相同名称的文件将被覆盖。您可以通过在本地复制S3存储桶的文件夹结构来避免这种情况。
import boto3
import os
from pathlib import Path
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket')
key = 'product/myproject/2021-02-15/'
objs = list(bucket.objects.filter(Prefix=key))
for obj in objs:
# print(obj.key)
# remove the file name from the object key
obj_path = os.path.dirname(obj.key)
# create nested directory structure
Path(obj_path).mkdir(parents=True, exist_ok=True)
# save file with full path locally
bucket.download_file(obj.key, obj.key)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句