question.it
question.it

Reputation: 2968

Find latest CSV File from S3 bucket using boto3, Python

I want to find latest .csv file from the S3 Bucket which contains multiple format file like .json, .xlsx, .csv, .txt files.

Client = boto3.client('s3', aws_access_key_id=S3_AccessKey, aws_secret_access_key=S3_SecretKey)
Response = Client.list_objects_v2(Bucket=S3_BucketName, Prefix=PrefixPath)

Files_ListS = Response.get('Contents')

Below script gives latest file from S3 and I am getting some .json file (bcz recently updated), I want to .csv file which is updated before json file, means latest from csv files.

max(Files_ListS , key=lambda x: x['LastModified'])

Upvotes: 2

Views: 2642

Answers (1)

jordanm
jordanm

Reputation: 34924

You can filter for only CSV files using a list comprehension by checking if the object keys end with .csv:

csv_objects = [f for f in Files_ListS if f['Key'].endswith('.csv')]
max(csv_objects, key=lambda x: x['LastModified'])

Upvotes: 2

Related Questions