Skip to content

Instantly share code, notes, and snippets.

@richarvey
Last active August 27, 2019 08:39
Show Gist options
  • Save richarvey/637cd595362760858496 to your computer and use it in GitHub Desktop.
Save richarvey/637cd595362760858496 to your computer and use it in GitHub Desktop.
Generate signed URL's for S3 objects from the command line (requires .boto file for credentials)
#!/usr/bin/env python3
import boto3, argparse
parser = argparse.ArgumentParser()
parser.add_argument('-b','--bucket', help='Name of your S3 Bucket', required=True)
parser.add_argument('-o','--object', help='Name of the object + prefix in your bucket', required=True)
parser.add_argument('-t','--time', type=int, help='Expirery in seconds Default = 60', default=60)
args = vars(parser.parse_args())
def get_signed_url(time, bucket, obj):
s3 = boto3.client('s3')
url = s3.generate_presigned_url('get_object', Params = { 'Bucket': bucket, 'Key': obj }, ExpiresIn = time)
return url
try:
url = get_signed_url(args['time'],args['bucket'], args['object'])
print(url)
except:
print('Something odd happened')
@richarvey
Copy link
Author

Updated for boto3 and python3 👯‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment