@@ -60,9 +60,13 @@ class S3FileMetadata(
60
60
tables.
61
61
62
62
"""
63
-
64
63
__tablename__ = "s3_file_metadata"
65
64
65
+ # For applications using multiple buckets for different purposes
66
+ # e.g user generated content and then a public media bucket we store
67
+ # the bucket name so we know where to target
68
+ bucket_name : Mapped [str ]
69
+
66
70
# This is the unique key for this object store in the associated bucket
67
71
# which is automatically assigned to the metadata, you do not have to
68
72
# worry about it simply deal with the wrapped methods provided by this class
@@ -179,12 +183,23 @@ def disable_legal_hold(self) -> bool:
179
183
180
184
181
185
@event .listens_for (S3FileMetadata , 'init' )
182
- def assigned_s3_key (target , args , kwargs ):
186
+ def assign_s3_key (target , args , kwargs ):
183
187
""" Assigns a UUID based on the UUID4 standard as the key for the file upload
184
188
185
189
When the application assigns a new S3FileMetadata object, it will be
186
190
given a UUID to use as the key in the bucket, and this record will act
187
191
as the meta table for translating the object in the bucket to a downloadable
188
192
file.
193
+
194
+ Suffix ensures that the file extension is preserved if it is provided in the
195
+ file_name. While for files that are accessed by presigned_urls this is not
196
+ required, it is useful for files that are accessed by the public.
189
197
"""
190
- target .s3_key = uuid4 ().hex
198
+ suffix = ""
199
+
200
+ if 'file_name' in kwargs :
201
+ file_name_split = kwargs ['file_name' ].split ('.' )
202
+ if len (file_name_split ) > 1 :
203
+ suffix = "." + file_name_split [- 1 ]
204
+
205
+ target .s3_key = uuid4 ().hex + suffix
0 commit comments