-
-
Save brunomichetti/a2ea087b8369f5c751a3da2586af100e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# some utils file for boto3 functions | |
from boto3 import Session | |
from typing import Any, Dict | |
from django.conf import settings | |
def get_object_key_from_url(url: str) -> str: | |
''' | |
Returns the object key for the given url | |
''' | |
endpoint = settings.AWS_S3_ENDPOINT_URL | |
bucket_name = settings.AWS_STORAGE_BUCKET_NAME | |
return url.split(f'{endpoint}/{bucket_name}/')[1] | |
def get_presigned_url(object_key: str) -> str: | |
''' | |
Returns a presigned URL for the given object key | |
''' | |
session = Session( | |
aws_access_key_id=settings.AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, | |
region_name=settings.AWS_S3_REGION_NAME, | |
) | |
s3client = session.client('s3') | |
url = s3client.generate_presigned_url( | |
ClientMethod='get_object', | |
Params={ | |
'Bucket': settings.AWS_STORAGE_BUCKET_NAME, | |
'Key': object_key, | |
}, | |
ExpiresIn=settings.AWS_PRESIGNED_URL_EXPIRATION_TIME_MINUTES, | |
) | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment