Reputation: 2968
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
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