Open source video conferencing app powered by LiveKit. Built with Django and React.
♻️(backend) factorize s3 client creation in utils
leo2c5dd151f129
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0b3e52a1..2d96bc6d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -20,6 +20,7 @@ and this project adheres to- ⬆️(frontend) upgrade posthog-js from 1.409.5 to 1.414.0- ⬆️(frontend) upgrade @fontsource-variable/lexend from 5.2.11 to 5.3.0- ⬆️(frontend) upgrade @fontsource/opendyslexic from 5.2.5 to 5.3.0+- ♻️(backend) factorize s3 client creation in utils### Fixeddiff --git a/src/backend/core/utils.py b/src/backend/core/utils.pyindex 5e113f91..6038f449 100644--- a/src/backend/core/utils.py+++ b/src/backend/core/utils.py@@ -381,25 +381,19 @@ def detect_mimetype(file_buffer: bytes, filename: str | None = None) -> str:return mimetype_from_content or "application/octet-stream"-def generate_upload_policy(file):+def _get_s3_client(*, override_domain: bool = True):+ """Return an S3 client, honoring the AWS_S3_DOMAIN_REPLACE endpoint override.++ AWS_S3_DOMAIN_REPLACE is used when the backend and frontend reach object+ storage under different domains (this is the case in the docker compose stack+ used in development: the frontend connects to the object storage on localhost+ while the backend uses the object storage service name declared in the stack).+ The domain name is used to compute the signature, so it can't be changed+ dynamically by the frontend; we build a dedicated boto3 client pointed at that+ endpoint. Otherwise we reuse the default storage client."""- Generate a S3 upload policy for a given file.-- Notes:- Originally taken from https://github.com/suitenumerique/drive/blob/564822d31f071c6dfacd112ef4b7146c73077cd9/src/backend/core/api/utils.py#L102 # pylint: disable=line-too-long- """-- key = file.temporary_file_key-- # This settings should be used if the backend application and the frontend application- # can't connect to the object storage with the same domain. This is the case in the- # docker compose stack used in development. The frontend application will use localhost- # to connect to the object storage while the backend application will use the object storage- # service name declared in the docker compose stack.- # This is needed because the domain name is used to compute the signature. So it can't be- # changed dynamically by the frontend application.- if settings.AWS_S3_DOMAIN_REPLACE:- s3_client = boto3.client(+ if settings.AWS_S3_DOMAIN_REPLACE and override_domain:+ return boto3.client("s3",aws_access_key_id=settings.AWS_S3_ACCESS_KEY_ID,aws_secret_access_key=settings.AWS_S3_SECRET_ACCESS_KEY,@@ -409,8 +403,20 @@ def generate_upload_policy(file):signature_version=settings.AWS_S3_SIGNATURE_VERSION,),)- else:- s3_client = default_storage.connection.meta.client+ return default_storage.connection.meta.client+++def generate_upload_policy(file):+ """+ Generate a S3 upload policy for a given file.++ Notes:+ Originally taken from https://github.com/suitenumerique/drive/blob/564822d31f071c6dfacd112ef4b7146c73077cd9/src/backend/core/api/utils.py#L102 # pylint: disable=line-too-long+ """++ key = file.temporary_file_key++ s3_client = _get_s3_client()# Generate the policypolicy = s3_client.generate_presigned_url(@@ -431,26 +437,7 @@ def generate_download_s3_url(if not key:raise ValueError("key cannot be empty")- # This setting should be used if the backend application and the frontend application- # can't connect to the object storage with the same domain. This is the case in the- # docker compose stack used in development. The frontend application will use localhost- # to connect to the object storage while the backend application will use the object storage- # service name declared in the docker compose stack.- # This is needed because the domain name is used to compute the signature. So it can't be- # changed dynamically by the frontend application.- if settings.AWS_S3_DOMAIN_REPLACE and override_domain:- s3_client = boto3.client(- "s3",- aws_access_key_id=settings.AWS_S3_ACCESS_KEY_ID,- aws_secret_access_key=settings.AWS_S3_SECRET_ACCESS_KEY,- endpoint_url=settings.AWS_S3_DOMAIN_REPLACE,- config=botocore.client.Config(- region_name=settings.AWS_S3_REGION_NAME,- signature_version=settings.AWS_S3_SIGNATURE_VERSION,- ),- )- else:- s3_client = default_storage.connection.meta.client+ s3_client = _get_s3_client(override_domain=override_domain)return s3_client.generate_presigned_url(ClientMethod="get_object",