Created
April 12, 2016 02:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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