Skip to content

Instantly share code, notes, and snippets.

@Y4suyuki
Created December 20, 2017 03:34
Show Gist options
  • Save Y4suyuki/e8a9611539538fe10995d4d7e8439a39 to your computer and use it in GitHub Desktop.
Save Y4suyuki/e8a9611539538fe10995d4d7e8439a39 to your computer and use it in GitHub Desktop.
from datetime import datetime
import boto3
def list_bucket_contents(bucket_name):
"""return a list of contents in a bucket"""
client = boto3.client('s3')
paginator = client.get_paginator('list_objects')
try:
page_iterator = paginator.paginate(Bucket=bucket_name)
except:
print(bucket_name)
return []
def page2csv(page):
contents = page['Contents']
return [','.join([c['LastModified'].strftime('%Y-%m-%d %H:%M'),
c['Key'],
str(c['Size'])]) for c in contents]
return sum([page2csv(p) for p in page_iterator], [])
if __name__ == '__main__':
client = boto3.client('s3')
response = client.list_buckets()
bucket_names = [b['Name'] for b in response['Buckets']]
bucket_names = ['foo']
for bucket_name in bucket_names:
with open(bucket_name + '_contents.csv', 'w') as f:
contents = list_bucket_contents(bucket_name)
f.write('\n'.join(contents))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment