-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
27 lines (23 loc) · 1.04 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import logging
import os
import boto3
from botocore.exceptions import ClientError
def create_presigned_url(object_name):
"""Generate a presigned URL to share an S3 object with a capped expiration of 60 seconds
:param object_name: string
:return: Presigned URL as string. If error, returns None.
"""
s3_client = boto3.client('s3',
region_name=os.environ.get('S3_PERSISTENCE_REGION'),
config=boto3.session.Config(signature_version='s3v4',s3={'addressing_style': 'path'}))
try:
bucket_name = os.environ.get('S3_PERSISTENCE_BUCKET')
response = s3_client.generate_presigned_url('get_object',
Params={'Bucket': bucket_name,
'Key': object_name},
ExpiresIn=60*1)
except ClientError as e:
logging.error(e)
return None
# The response contains the presigned URL
return response