Created
December 7, 2012 19:39
-
-
Save dacamo76/4235890 to your computer and use it in GitHub Desktop.
Get keys recursively from S3
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
#!/usr/bin/env python | |
import boto | |
import os | |
import sys | |
bucket_name = str(sys.argv[1]) | |
dirname = str(sys.argv[2]) | |
def files(keys): | |
return (key for key in keys if not key.name.endswith('/')) | |
s3 = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | |
bucket = s3.get_bucket(bucket_name) | |
keys = bucket.list(dirname) | |
for key in files(keys): | |
path, file = os.path.split(key.name) | |
print('Downloading {0}'.format((key.name))) | |
if not os.path.exists(path): | |
os.makedirs(path) | |
key.get_contents_to_filename(key.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment