如何使用Boto3下载S3存储桶的最新文件? [英] How to download the latest file of an S3 bucket using Boto3?

查看:152
本文介绍了如何使用Boto3下载S3存储桶的最新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能找到的其他问题是指Boto的较旧版本.我想下载S3存储桶的最新文件.在文档中,我发现是list_object_versions()的方法,可让您获得布尔IsLatest.不幸的是,我仅设法建立了连接并下载了文件.您能否告诉我如何扩展代码以获取存储桶的最新文件?谢谢

The other questions I could find were refering to an older version of Boto. I would like to download the latest file of an S3 bucket. In the documentation I found that there is a method list_object_versions() that gets you a boolean IsLatest. Unfortunately I only managed to set up a connection and to download a file. Could you please show me how I can extend my code to get the latest file of the bucket? Thank you

import boto3
conn = boto3.client('s3',
                    region_name="eu-west-1",
                    endpoint_url="customendpoint",
                    config=Config(signature_version="s3", s3={'addressing_style': 'path'}))

从这里我不知道如何从名为mytestbucket的存储桶中获取最新添加的文件.存储桶中有各种csv文件,但是它们的名称当然都是不同的.

From here I dont know how to get the latest added file from a bucket called mytestbucket. There are various csv files in the bucket but all of course with a different name.

更新:

import boto3
from botocore.client import Config

s3 = boto3.resource('s3', region_name="eu-west-1", endpoint_url="custom endpoint", aws_access_key_id = '1234', aws_secret_access_key = '1234', config=Config(signature_version="s3", s3={'addressing_style': 'path'}))
my_bucket = s3.Bucket('mytestbucket22')
unsorted = []
for file in my_bucket.objects.filter():
   unsorted.append(file)

files = [obj.key for obj in sorted(unsorted, key=get_last_modified, reverse=True)][0:9]

这给了我以下错误:

NameError: name 'get_last_modified' is not defined

推荐答案

我提供的答案的变化形式:

Variation of the answer I provided for: Boto3 S3, sort bucket by last modified. You can modify the code to suit to your needs.

get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))

s3 = boto3.client('s3')
objs = s3.list_objects_v2(Bucket='my_bucket')['Contents']
last_added = [obj['Key'] for obj in sorted(objs, key=get_last_modified)][0]

如果要反转排序:

[obj['Key'] for obj in sorted(objs, key=get_last_modified, reverse=True)][0]

这篇关于如何使用Boto3下载S3存储桶的最新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆