Created
March 30, 2018 14:37
-
-
Save jpigree/b5aeb5c5dcec36b8ffd19c92367896a0 to your computer and use it in GitHub Desktop.
Generate an S3 signed 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 | |
# Tested with Python 3 | |
# Needs boto3 installed (pip install --user boto3) | |
import optparse | |
import sys | |
import boto3 | |
def sign(bucket, path, expiry): | |
s3 = boto3.client('s3') | |
return s3.generate_presigned_url( | |
ClientMethod='get_object', | |
Params={ | |
'Bucket': bucket, | |
'Key': path, | |
}, | |
ExpiresIn=int(expiry), | |
HttpMethod='GET' | |
) | |
if __name__ == '__main__': | |
parser = optparse.OptionParser() | |
parser.add_option('-b', '--bucket', dest='bucket', help='S3 bucket containing the file') | |
parser.add_option('-p', '--path', dest='path', help='Path to the file (relative to the bucket)') | |
parser.add_option('--expiry', dest='expiry', default='315360000', help='Expiry time, in seconds (defaults to ten years)') | |
options, args = parser.parse_args() | |
for opt in ('bucket', 'path'): | |
assert options.__dict__.get(opt), '%s is not optional' % opt | |
print(sign( | |
bucket=options.bucket, | |
path=options.path, | |
expiry=options.expiry | |
)) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment