From f12708acee3377187f0ff5fb5ccb2e65f0256bc6 Mon Sep 17 00:00:00 2001 From: Samuel Paccoud - DINUM Date: Mon, 19 Aug 2024 22:28:36 +0200 Subject: [PATCH] =?UTF-8?q?=E2=AC=86=EF=B8=8F(backend)=20upgrade=20boto3?= =?UTF-8?q?=20to=201.14.4=20for=20unsigned=20urls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For media urls, we want to compute authorization as a header instead of computing signed urls. The url of a media file can then be computed without the querystring authorization part. This requires upgrading django-storages to the 1.14 version to benefit from the "unsigned connection" in the S3Storage backend. --- src/backend/core/models.py | 15 +++++++++++---- src/backend/pyproject.toml | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/backend/core/models.py b/src/backend/core/models.py index 94d84cfe..d00b66de 100644 --- a/src/backend/core/models.py +++ b/src/backend/core/models.py @@ -324,16 +324,23 @@ class Document(BaseModel): file_key = self.file_key bytes_content = self._content.encode("utf-8") - if default_storage.exists(file_key): + # Attempt to directly check if the object exists using the storage client. + try: response = default_storage.connection.meta.client.head_object( Bucket=default_storage.bucket_name, Key=file_key ) + except ClientError as excpt: + # If the error is a 404, the object doesn't exist, so we should create it. + if excpt.response["Error"]["Code"] == "404": + has_changed = True + else: + raise + else: + # Compare the existing ETag with the MD5 hash of the new content. has_changed = ( response["ETag"].strip('"') - != hashlib.md5(bytes_content).hexdigest() # noqa + != hashlib.md5(bytes_content).hexdigest() # noqa: S324 ) - else: - has_changed = True if has_changed: content_file = ContentFile(bytes_content) diff --git a/src/backend/pyproject.toml b/src/backend/pyproject.toml index 82f3e286..c829e495 100644 --- a/src/backend/pyproject.toml +++ b/src/backend/pyproject.toml @@ -34,7 +34,7 @@ dependencies = [ "django-parler==2.3", "redis==5.0.8", "django-redis==5.4.0", - "django-storages[s3]==1.14.2", + "django-storages[s3]==1.14.4", "django-timezone-field>=5.1", "django==5.0.8", "djangorestframework==3.15.2",