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",