Skip to content

Instantly share code, notes, and snippets.

@qpxu007
Forked from manelclos/s3signurl.py
Created September 28, 2015 23:07
Show Gist options
  • Select an option

  • Save qpxu007/e1f16c3b9774aaf29d13 to your computer and use it in GitHub Desktop.

Select an option

Save qpxu007/e1f16c3b9774aaf29d13 to your computer and use it in GitHub Desktop.

Revisions

  1. @manelclos manelclos revised this gist Sep 23, 2015. 1 changed file with 11 additions and 6 deletions.
    17 changes: 11 additions & 6 deletions s3signurl.py
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,14 @@
    #!/usr/bin/env python
    import optparse
    import sys
    import os

    os.environ['S3_USE_SIGV4'] = 'True'

    from boto.s3.connection import S3Connection

    def sign(bucket, path, access_key, secret_key, https, expiry):
    c = S3Connection(access_key, secret_key)
    def sign(bucket, path, access_key, secret_key, https, expiry, host=None):
    c = S3Connection(access_key, secret_key, host=host)
    return c.generate_url(
    expires_in=long(expiry),
    method='GET',
    @@ -23,18 +26,20 @@ def sign(bucket, path, access_key, secret_key, https, expiry):
    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)')

    parser.add_option('--host', dest='host', default=None, help='AWS host')

    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)
    expiry=long(options.expiry),
    host=options.host
    )
    sys.exit(0)
  2. @obeattie obeattie revised this gist Jul 19, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions s3signurl.py
    Original file line number Diff line number Diff line change
    @@ -37,3 +37,4 @@ def sign(bucket, path, access_key, secret_key, https, expiry):
    https=options.https,
    expiry=long(options.expiry)
    )
    sys.exit(0)
  3. @obeattie obeattie created this gist Jul 19, 2011.
    39 changes: 39 additions & 0 deletions s3signurl.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/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)
    )