如何使用boto3下载该文件夹中的所有内容

2024-05-29 03:09:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想下载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


Tags: 文件csv方法keyimport文件夹clientobj
3条回答

因为您正在使用resource,所以您可以使用download_file

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:
    #print(obj.key)
    out_name = obj.key.split('/')[-1]
    bucket.download_file(obj.key, out_name)  

Filter返回集合对象,而不仅仅是名称,^{}方法需要对象名称:

试试这个:

objs = list(bucket.objects.filter(Prefix=key))
client = boto3.client('s3')
for obj in objs:
    client.download_file(bucket, obj.name, obj.name)

您还可以使用print(obj)打印循环中的obj对象,以查看它实际拥有的内容

Marcin的回答是正确的,但不同路径中具有相同名称的文件将被覆盖。 您可以通过在本地复制S3 bucket的文件夹结构来避免这种情况

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)

相关问题 更多 >

    热门问题