Skip to content

Instantly share code, notes, and snippets.

@premchalmeti
Created August 26, 2020 14:33
Show Gist options
  • Save premchalmeti/4d5c9ef09053acb195da5e248ff476a0 to your computer and use it in GitHub Desktop.
Save premchalmeti/4d5c9ef09053acb195da5e248ff476a0 to your computer and use it in GitHub Desktop.
This function takes a S3 bucket name and optionally folder name and returns a sorted list of objects inside s3
def list_objects(
bucket_name: str, folder_name: str
) -> None:
""" This function takes a S3 bucket name and optionally folder name
and returns a sorted list of objects inside S3
Args:
bucket_name ([string]): Name of the S3 bucket
folder_name ([string]): Sub folder inside S3 (optional)
"""
import boto3
s3_client = boto3.client('s3')
response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=folder_name)
objs = response['Contents']
objs.sort(key=lambda k:k['LastModified'])
print("%10s | %10s | %20s" % ('Date', 'Bucket', 'File'))
for obj in objs:
print("%10s | %10s | %20s" % (obj['LastModified'].strftime("%Y-%m-%d"), bucket_name, obj['Key']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment