Skip to content

Commit 5a19dbf

Browse files
committed
refactor: legal_hold implementation of objects
fixes various method calls for legal_hold and ensures that the state of the object is updated based on if the legal hold succeeds
1 parent d902e54 commit 5a19dbf

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

src/labs/models/s3.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
"""
55
from uuid import uuid4
66
from datetime import timedelta
7-
from typing import TYPE_CHECKING, Union
7+
from typing import Union
88

99
from sqlalchemy import event
10-
from sqlalchemy.orm import relationship, mapped_column, Mapped
10+
from sqlalchemy.orm import relationship,\
11+
mapped_column, Mapped
1112

12-
from .utils import DateTimeMixin, IdentifierMixin, ModelCRUDMixin, fk_user_uuid
13+
from .utils import DateTimeMixin, IdentifierMixin,\
14+
ModelCRUDMixin, fk_user_uuid
1315

1416
from ..db import Base
1517
from ..utils import minio_client
@@ -60,14 +62,19 @@ class S3FileMetadata(
6062

6163
mime_type: Mapped[str]
6264

65+
# Is set to True if the object is deemed to be logically deleted
66+
# see other part of the documentation for logical and physical deletion policy
6367
deleted: Mapped[bool] = mapped_column(
6468
default=False
6569
)
6670

71+
# Is set to True if the object is currently in held in a legal hold
6772
legal_hold: Mapped[bool] = mapped_column(
6873
default=False
6974
)
7075

76+
# Is set of True once the application is able to validate
77+
# the presence of the object on the store
7178
is_valid: Mapped[bool] = mapped_column(
7279
default=False
7380
)
@@ -81,7 +88,10 @@ class S3FileMetadata(
8188

8289
@property
8390
def presigned_download_url(self) -> Union[str, None]:
84-
"""
91+
""" Provides a presigned download url for the object in the store
92+
93+
If there is an issue then you will receive None, the application should not proceed
94+
with the download if this is the case.
8595
"""
8696
try:
8797
return minio_client.presigned_get_object(
@@ -100,8 +110,16 @@ def presigned_download_url(self) -> Union[str, None]:
100110

101111
@property
102112
def presigned_upload_url(self) -> Union[str, None]:
103-
"""
104-
113+
""" Use this property to get a URL to create or update the object
114+
115+
Note that if versioning is not turned on for the object store then this will
116+
replace the contents of the object on the store.
117+
118+
Once created you should send this to the browser to action the upload, the
119+
URL is signed for particular amount of time and will expire post that.
120+
121+
If you receive None then there is an issue with the object store and you
122+
should not proceed with the upload.
105123
"""
106124
try:
107125
url = minio_client.presigned_put_object(
@@ -116,30 +134,46 @@ def presigned_upload_url(self) -> Union[str, None]:
116134
return None
117135

118136
def enable_legal_hold(self) -> bool:
137+
""" The Object Lock legal hold operation enables you to place a legal hold
138+
on an object version. Like setting a retention period, a legal hold prevents an
139+
object version from being overwritten or deleted.
140+
141+
This is a method which is used to enable a legal hold on the object, the
142+
object must exists in the store and must not already have a legal hold.
143+
"""
119144
try:
120-
minio_client.put_object_legal_hold(
145+
minio_client.enable_object_legal_hold(
121146
config.S3_BUCKET_NAME,
122147
self.s3_key,
123-
True
124148
)
149+
self.legal_hold = True
125150
return True
126151
except Exception:
152+
self.legal_hold = False
127153
return False
128154

129155
def disable_legal_hold(self) -> bool:
156+
""" Disables the legal hold if one is placed on the object.
157+
158+
Note that this method will return a boolean to state the success of the
159+
operation. This is not the legal hold status of the object.
160+
"""
130161
try:
131-
minio_client.put_object_legal_hold(
162+
minio_client.disable_object_legal_hold(
132163
config.S3_BUCKET_NAME,
133164
self.s3_key,
134-
False
135165
)
166+
167+
self.legal_hold = False
168+
136169
return True
137170
except Exception:
171+
self.legal_hold = True
138172
return False
139173

140174

141175
@event.listens_for(S3FileMetadata, 'init')
142-
def receive_init(target, args, kwargs):
176+
def assigned_s3_key(target, args, kwargs):
143177
""" Assigns a UUID based on the UUID4 standard as the key for the file upload
144178
145179
When the application assigns a new S3FileMetadata object, it will be
@@ -149,3 +183,4 @@ def receive_init(target, args, kwargs):
149183
"""
150184
target.s3_key = uuid4().hex
151185
target.prefix = uuid4().hex
186+

0 commit comments

Comments
 (0)