Skip to content

Instantly share code, notes, and snippets.

@jpigree
Created March 30, 2018 14:37
Show Gist options
  • Save jpigree/b5aeb5c5dcec36b8ffd19c92367896a0 to your computer and use it in GitHub Desktop.
Save jpigree/b5aeb5c5dcec36b8ffd19c92367896a0 to your computer and use it in GitHub Desktop.
Generate an S3 signed URL
#!/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