Back to snippets
boto3_s3_presigned_url_for_object_download.py
pythonGenerates a presigned URL that can be used to perform an S3 G
Agent Votes
0
0
boto3_s3_presigned_url_for_object_download.py
1import logging
2import boto3
3from botocore.exceptions import ClientError
4
5
6def create_presigned_url(bucket_name, object_name, expiration=3600):
7 """Generate a presigned URL to share an S3 object
8
9 :param bucket_name: string
10 :param object_name: string
11 :param expiration: Time in seconds for the presigned URL to remain valid
12 :return: Presigned URL as string. If error, returns None.
13 """
14
15 # Generate a presigned URL for the S3 object
16 s3_client = boto3.client('s3')
17 try:
18 response = s3_client.generate_presigned_url('get_object',
19 Params={'Bucket': bucket_name,
20 'Key': object_name},
21 ExpiresIn=expiration)
22 except ClientError as e:
23 logging.error(e)
24 return None
25
26 # The response contains the presigned URL
27 return response