Created
October 1, 2015 00:59
-
-
Save dlobue/79b34ee6f7c93b24dae2 to your computer and use it in GitHub Desktop.
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
from argparse import ArgumentParser | |
from urlparse import urlparse | |
import boto | |
def get_temp_url(bucket, key, expires_in, upload): | |
s3conn = boto.connect_s3() | |
bucket = s3conn.get_bucket(bucket) | |
key = bucket.get_key(key, validate=False) | |
url = key.generate_url(expires_in, method='PUT' if upload else 'GET') | |
return url | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument('-e', '--expires_in', default=3600, type=int, | |
help='how long url is valid for in seconds') | |
parser.add_argument('-u', '--upload', default=False, | |
action='store_true', | |
help='create url for uploading file (default is to create a url meant for downloading') | |
parser.add_argument('s3url', help='location of object in url style (ex: s3://bucket-name/path/to/key)') | |
ns = parser.parse_args() | |
parseds3loc = urlparse(ns.s3url) | |
bucket = parseds3loc.netloc | |
key = parseds3loc.path | |
tempurl = get_temp_url(bucket, key, ns.expires_in, ns.upload) | |
print(tempurl) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment