4
4
"""
5
5
from uuid import uuid4
6
6
from datetime import timedelta
7
- from typing import TYPE_CHECKING , Union
7
+ from typing import Union
8
8
9
9
from sqlalchemy import event
10
- from sqlalchemy .orm import relationship , mapped_column , Mapped
10
+ from sqlalchemy .orm import relationship ,\
11
+ mapped_column , Mapped
11
12
12
- from .utils import DateTimeMixin , IdentifierMixin , ModelCRUDMixin , fk_user_uuid
13
+ from .utils import DateTimeMixin , IdentifierMixin ,\
14
+ ModelCRUDMixin , fk_user_uuid
13
15
14
16
from ..db import Base
15
17
from ..utils import minio_client
@@ -60,14 +62,19 @@ class S3FileMetadata(
60
62
61
63
mime_type : Mapped [str ]
62
64
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
63
67
deleted : Mapped [bool ] = mapped_column (
64
68
default = False
65
69
)
66
70
71
+ # Is set to True if the object is currently in held in a legal hold
67
72
legal_hold : Mapped [bool ] = mapped_column (
68
73
default = False
69
74
)
70
75
76
+ # Is set of True once the application is able to validate
77
+ # the presence of the object on the store
71
78
is_valid : Mapped [bool ] = mapped_column (
72
79
default = False
73
80
)
@@ -81,7 +88,10 @@ class S3FileMetadata(
81
88
82
89
@property
83
90
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.
85
95
"""
86
96
try :
87
97
return minio_client .presigned_get_object (
@@ -100,8 +110,16 @@ def presigned_download_url(self) -> Union[str, None]:
100
110
101
111
@property
102
112
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.
105
123
"""
106
124
try :
107
125
url = minio_client .presigned_put_object (
@@ -116,30 +134,46 @@ def presigned_upload_url(self) -> Union[str, None]:
116
134
return None
117
135
118
136
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
+ """
119
144
try :
120
- minio_client .put_object_legal_hold (
145
+ minio_client .enable_object_legal_hold (
121
146
config .S3_BUCKET_NAME ,
122
147
self .s3_key ,
123
- True
124
148
)
149
+ self .legal_hold = True
125
150
return True
126
151
except Exception :
152
+ self .legal_hold = False
127
153
return False
128
154
129
155
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
+ """
130
161
try :
131
- minio_client .put_object_legal_hold (
162
+ minio_client .disable_object_legal_hold (
132
163
config .S3_BUCKET_NAME ,
133
164
self .s3_key ,
134
- False
135
165
)
166
+
167
+ self .legal_hold = False
168
+
136
169
return True
137
170
except Exception :
171
+ self .legal_hold = True
138
172
return False
139
173
140
174
141
175
@event .listens_for (S3FileMetadata , 'init' )
142
- def receive_init (target , args , kwargs ):
176
+ def assigned_s3_key (target , args , kwargs ):
143
177
""" Assigns a UUID based on the UUID4 standard as the key for the file upload
144
178
145
179
When the application assigns a new S3FileMetadata object, it will be
@@ -149,3 +183,4 @@ def receive_init(target, args, kwargs):
149
183
"""
150
184
target .s3_key = uuid4 ().hex
151
185
target .prefix = uuid4 ().hex
186
+
0 commit comments