Created
October 21, 2015 19:31
-
-
Save nmagee/d4b5585689e0a1c449c5 to your computer and use it in GitHub Desktop.
Sign S3 URL
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 | |
# http://boto3.readthedocs.org/en/latest/reference/services/rds.html | |
import boto3 | |
import sys, getopt | |
bucket = None | |
file_path = None | |
timeout_seconds = 3600 | |
def print_usage(): | |
print """ | |
s3-temp-url.py -b bucket -f file-path -t [ timeout-seconds ] | |
Prints out a temporary url to S3 resource that expires in timeout-seconds. | |
bucket and file-path are required. | |
timeout-seconds defaults to 3600 -- one hour. | |
http://boto3.readthedocs.org/en/latest/reference/services/rds.html""" | |
sys.exit(2) | |
def parse_args(argv): | |
global bucket, timeout_seconds, file_path | |
try: | |
opts, args = getopt.getopt( | |
argv, | |
"h:b:f:t:", | |
["help=","bucket=","file-path=","timeout-seconds="]) | |
except getopt.GetoptError: | |
print_usage() | |
for opt, arg in opts: | |
if opt in ("-h", "--help"): | |
print_usage() | |
elif opt in ("-b", "--bucket"): | |
bucket = arg | |
elif opt in ("-t", "--timeout-seconds"): | |
timeout_seconds = int(arg) | |
elif opt in ("-f", "--file-path"): | |
file_path = arg | |
if (bucket is None) or (file_path is None): | |
print_usage() | |
def print_tmp_url(): | |
client = boto3.client('s3') | |
args = { | |
'ClientMethod': 'get_object', | |
'Params': { 'Bucket': bucket, 'Key': file_path }, | |
'ExpiresIn': timeout_seconds | |
} | |
# print args | |
response = client.generate_presigned_url(**args); | |
msg = ''' | |
This url will expire in {timeout_seconds} seconds. | |
{the_url} | |
''' | |
print msg.format(timeout_seconds=timeout_seconds, the_url=response) | |
if __name__ == "__main__": | |
parse_args(sys.argv[1:]) | |
print_tmp_url() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment