How to get latest file from s3 bucket python

To get the latest file from an S3 bucket using Python, you can use the AWS SDK for Python (Boto3) library. Here’s a step-by-step guide:

  • 1. Install the Boto3 library using pip: pip install boto3
  • 2. Import the necessary Boto3 module and create an S3 client:
    
import boto3

# Replace 'your_access_key' and 'your_secret_key' with your AWS credentials
s3 = boto3.client('s3', aws_access_key_id='your_access_key', aws_secret_access_key='your_secret_key')
    
  
  • 3. Get the list of objects/files in the S3 bucket:
    
bucket_name = 'your_bucket_name'
response = s3.list_objects_v2(Bucket=bucket_name)

# Retrieve the list of files
files = response['Contents']
    
  
  • 4. Sort the list of files by the ‘LastModified’ attribute in descending order:
    
sorted_files = sorted(files, key=lambda x: x['LastModified'], reverse=True)
    
  
  • 5. The first element in the sorted list will be the latest file:
    
latest_file = sorted_files[0]
file_name = latest_file['Key']
    
  

Now, file_name will contain the name of the latest file in the S3 bucket. You can use this information to download or perform any other operations on the file.

Here’s an example for better understanding:

    
import boto3

# Replace 'your_access_key' and 'your_secret_key' with your AWS credentials
s3 = boto3.client('s3', aws_access_key_id='your_access_key', aws_secret_access_key='your_secret_key')

bucket_name = 'your_bucket_name'
response = s3.list_objects_v2(Bucket=bucket_name)
files = response['Contents']

sorted_files = sorted(files, key=lambda x: x['LastModified'], reverse=True)

latest_file = sorted_files[0]
file_name = latest_file['Key']

print("Latest File Name:", file_name)
    
  

Make sure to replace ‘your_access_key’, ‘your_secret_key’, and ‘your_bucket_name’ with your actual AWS credentials and S3 bucket name.

Leave a comment