Last active
August 29, 2015 14:05
-
-
Save gavsmi/56d199a46c9a94223787 to your computer and use it in GitHub Desktop.
Download file from S3
This file contains hidden or 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/python | |
from boto.utils import get_instance_metadata | |
import boto | |
import argparse, logging | |
def parsed_args(): | |
# parse cmd line args | |
parser = argparse.ArgumentParser(description='Download file from S3') | |
parser.add_argument('--bucket', | |
help='Name of S3 bucket to download from') | |
parser.add_argument('--file', | |
help='Name of file to download') | |
return parser.parse_args(); | |
def main(): | |
logging.info('Fetching file from S3...') | |
args = parsed_args(); | |
conn = boto.s3.connect_to_region(current_region()) | |
bucket = conn.get_bucket(args.bucket) | |
logging.info('Iterating over bucket %s contents...' % args.bucket) | |
for l in bucket.list(): | |
keyString = str(l.key) | |
logging.info('Found file %s' % keyString) | |
if keyString == args.file: | |
logging.info('Downloading %s' % keyString) | |
l.get_contents_to_filename('/tmp/'+keyString[keyString.rfind('/'):]) | |
def current_zone(): | |
metadata = get_instance_metadata() | |
return metadata['placement']['availability-zone'] | |
def current_region(): | |
zone = current_zone() | |
return zone[:-1] | |
if __name__ == '__main__': | |
logging.getLogger().setLevel(logging.INFO) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment