Skip to content

Instantly share code, notes, and snippets.

@makuk66
Last active December 11, 2015 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save makuk66/4530657 to your computer and use it in GitHub Desktop.
Save makuk66/4530657 to your computer and use it in GitHub Desktop.
quick and dirty s3 download script using boto, which knows how to deal with EC2 IAM roles for machines.
#!/usr/bin/env python
#
# s3-download.py - a quick-and-dirty download script to download from S3
# implemented using Boto, which automatically applies AWS IAM role credentials
#
# This code is distributed under the MIT license from
# http://opensource.org/licenses/MIT:
#
# Copyright (c) 2013 Martijn Koster
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import urllib2
import boto
import re
import sys
from boto.s3.key import Key
if len(sys.argv) != 3:
print "Usage: s3-download.py s3://bucket/path localfile"
sys.exit(-1)
s3_url=sys.argv[1]
installer_jar=sys.argv[2]
r=re.search('s3://([^/]+)(.*)', s3_url)
if r == None:
raise "Malformed S3 URL"
s3_bucket=r.group(1)
s3_path=r.group(2)
c = boto.connect_s3()
b = c.get_bucket(s3_bucket)
k = Key(b)
k.key = s3_path
print "downloading from S3 {0} to {1}".format(s3_url, installer_jar)
k.get_contents_to_filename(installer_jar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment