Home > database >  Get filename from S3 bucket path
Get filename from S3 bucket path

Time:02-05

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?

CodePudding user response:

Split on / and get the last element:

sorted_contents[0].get('Key').split("/")[-1]

CodePudding user response:

Split on "/" will help you with this:

S3_Key.split("/")[-1]

Sample code:

import os

folder_path = "folder/sub/2023-02-03_myfile.csv"

file_name = os.path.basename(folder_path)
last_name = file_name.split("/")[-1]

print(last_name)

OUTPUT: 2023-02-03_myfile.csv

Ref: https://www.cloudkaramchari.com/blog/restart-all-aws-ecs-services-using-lambda/

  • Related