AWS S3 : List files using Python code

Below is a simple python code to list files under a specific directory in s3 bucket. You should have boto3 installed.

import boto3
s3=boto3.client('s3')
#Modify the bucket name 
s3Bucket = 'MY_BUCKET'
#Prefix should contain the base directory whose files you want to list
prefix = 'tmp/'
objects=s3.list_objects(Bucket=s3Bucket,Prefix=prefix)
if 'Contents' in objects:
    contents = objects['Contents']
    for file in contents:
        print(file['Key'])

Leave a Reply

Your email address will not be published. Required fields are marked *