Skip to content

Latest commit

 

History

History
57 lines (46 loc) · 1.97 KB

S3.md

File metadata and controls

57 lines (46 loc) · 1.97 KB

Sign an S3 object URI

Example Usage - Expire an object 7 days in the future:

$ chmod +x ./s3signurl.py
$ ./s3signurl.py -b $BUCKET -p /path/to/object —expiry 604800 -a $AWS_ACCESS_KEY -s $AWS_SECRET_ACCESS_KEY
#!/usr/bin/env python
import optparse
import sys

from boto.s3.connection import S3Connection

def sign(bucket, path, access_key, secret_key, https, expiry):
    c = S3Connection(access_key, secret_key)
    return c.generate_url(
        expires_in=long(expiry),
        method='GET',
        bucket=bucket,
        key=path,
        query_auth=True,
        force_http=(not https)
    )

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('-a', '--access-key', dest='access_key', help='Your AWS Access Key ID')
    parser.add_option('-s', '--secret-key', dest='secret_key', help='Your AWS secret key')
    parser.add_option('--no-https', dest='https', action='store_false', default=True, help='Disable serving over HTTPS')
    parser.add_option('--expiry', dest='expiry', default='631138519', help='Expiry time, in seconds (defaults to two years)')
    
    options, args = parser.parse_args()
    
    for opt in ('bucket', 'path', 'access_key', 'secret_key'):
        assert options.__dict__.get(opt), '%s is not optional' % opt
    
    print sign(
        bucket=options.bucket,
        path=options.path,
        access_key=options.access_key,
        secret_key=options.secret_key,
        https=options.https,
        expiry=long(options.expiry)
    )
    sys.exit(0)

Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/