Skip to content

Instantly share code, notes, and snippets.

@alecbw
Created May 21, 2021 19:09
Show Gist options
  • Save alecbw/80b1e3b4a35436638f8f269a64ee171b to your computer and use it in GitHub Desktop.
Save alecbw/80b1e3b4a35436638f8f269a64ee171b to your computer and use it in GitHub Desktop.
Wrapper to generate a pre-signed URL for temporary access (up to 7 days) to a file in an otherwise inaccessible S3 bucket. Optionally supports S3 Transfer Acceleration
# DO NOTE: this will generate a url even if the file_name is not actually in the bucket
def generate_s3_presigned_url(bucket_name, file_name, **kwargs):
client = boto3.client(
's3',
config=Config(
signature_version='s3v4',
s3 = {'use_accelerate_endpoint': kwargs.get("accelerate_endpoint", False)}
)
)
url = client.generate_presigned_url(
ClientMethod='get_object',
Params={'Bucket': bucket_name, 'Key': file_name},
ExpiresIn=kwargs.get("TTL", 3600) # one hour
)
return url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment