Geen omschrijving

utils.py 1.0KB

123456789101112131415161718192021222324252627
  1. import logging
  2. import os
  3. import boto3
  4. from botocore.exceptions import ClientError
  5. def create_presigned_url(object_name):
  6. """Generate a presigned URL to share an S3 object with a capped expiration of 60 seconds
  7. :param object_name: string
  8. :return: Presigned URL as string. If error, returns None.
  9. """
  10. s3_client = boto3.client('s3',
  11. region_name=os.environ.get('S3_PERSISTENCE_REGION'),
  12. config=boto3.session.Config(signature_version='s3v4',s3={'addressing_style': 'path'}))
  13. try:
  14. bucket_name = os.environ.get('S3_PERSISTENCE_BUCKET')
  15. response = s3_client.generate_presigned_url('get_object',
  16. Params={'Bucket': bucket_name,
  17. 'Key': object_name},
  18. ExpiresIn=60*1)
  19. except ClientError as e:
  20. logging.error(e)
  21. return None
  22. # The response contains the presigned URL
  23. return response