I am getting the last modified file from S3 bucket using the below code:
import boto3
import urllib.parse
import json
import botocore.session as bc
import time
from time import mktime
from datetime import datetime
print('Loading function')
def lambda_handler(event, context):
s3_client = boto3.client("s3")
list_of_s3_objs = s3_client.list_objects_v2(Bucket="mybucket", Prefix="folder/sub/")
# Returns a bunch of json
contents = list_of_s3_objs["Contents"]
#get last modified
sorted_contents = sorted(list_of_s3_objs['Contents'], key=lambda d: d['LastModified'], reverse=True)
print(sorted_contents[0].get('Key'))
This prints the last modified file from the path ‘mybucket/folder/sub’ correctly. The output is:
folder/sub/2023-02-03_myfile.csv
How to extract just the filename ‘2023-02-03_myfile.csv’ file from this path?
2
Answers
Split on
/
and get the last element:Split on "/" will help you with this:
Sample code:
OUTPUT: 2023-02-03_myfile.csv
Ref: https://www.cloudkaramchari.com/blog/restart-all-aws-ecs-services-using-lambda/