Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnboxall/849c6e1f588a422b8bc6096aa5e8eed2 to your computer and use it in GitHub Desktop.
Save johnboxall/849c6e1f588a422b8bc6096aa5e8eed2 to your computer and use it in GitHub Desktop.
Example of using the `boto3` API to very slowly searching through a S3 bucket for objects which match a condition.
'''
Outputs JSON key / value pairs of objects in an S3 bucket which redirect.
* http://boto3.readthedocs.org/en/latest/guide/migrations3.html
* http://boto3.readthedocs.org/en/latest/reference/services/s3.html#S3.Object.get
'''
import csv
import json
import boto3
S3_BUCKET = 'www.mobify.com'
redirects = {}
s3 = boto3.resource('s3')
bucket = s3.Bucket(S3_BUCKET)
for page in bucket.objects.pages():
for key in page:
redirect = bucket.Object(key.key).get().get('WebsiteRedirectLocation',
None)
if redirect is None:
continue
redirects[key.key] = redirect
print(json.dumps(redirects, indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment