Skip to content

Instantly share code, notes, and snippets.

@mda590
Created April 27, 2017 17:23
Show Gist options
  • Save mda590/50e07e376b92fdd8d014c1bce69d525d to your computer and use it in GitHub Desktop.
Save mda590/50e07e376b92fdd8d014c1bce69d525d to your computer and use it in GitHub Desktop.
List all S3 Buckets and Objects
#
# Print all S3 buckets and objects
# Warning: If you have a lot of S3 objects, this could potentially incur a large cost.
#
import boto3
client = boto3.client('s3')
buckets = client.list_buckets()
for bucket in buckets["Buckets"]:
print(bucket['Name'])
keylist = client.list_objects_v2(Bucket=bucket["Name"], Delimiter='/')
if "Contents" in keylist:
for object in keylist.get('CommonPrefixes'):
print(object.get('Prefix'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment