Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

python - Get the last file name in a S3 folder?

I have a lot of files in the s3 bucket. The folder/file names are in the following format.

s3://bucket/folder/year=1990/1990-01-01.csv
......
s3://bucket/folder/year=2020/2021-01-23.csv

How to use Python to get the last file name? In the above example, it is 2021-01-23.csv.

question from:https://stackoverflow.com/questions/65896284/get-the-last-file-name-in-a-s3-folder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can check this alternative which uses filter:

import boto3

s3r = boto3.resource('s3')

all_files = list(s3r.Bucket('bucket').objects.filter(Prefix='folder/').all())

print(all_files[-1])

It does not do semantic check based on the dates in your files to find the last one. But the example could be extended to do it as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...