Skip to content

Commit a78f82c

Browse files
committed
feat: adds file extension to s3 keys
1 parent 21b2776 commit a78f82c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/labs/models/s3.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ class S3FileMetadata(
6060
tables.
6161
6262
"""
63-
6463
__tablename__ = "s3_file_metadata"
6564

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+
6670
# This is the unique key for this object store in the associated bucket
6771
# which is automatically assigned to the metadata, you do not have to
6872
# worry about it simply deal with the wrapped methods provided by this class
@@ -179,12 +183,23 @@ def disable_legal_hold(self) -> bool:
179183

180184

181185
@event.listens_for(S3FileMetadata, 'init')
182-
def assigned_s3_key(target, args, kwargs):
186+
def assign_s3_key(target, args, kwargs):
183187
""" Assigns a UUID based on the UUID4 standard as the key for the file upload
184188
185189
When the application assigns a new S3FileMetadata object, it will be
186190
given a UUID to use as the key in the bucket, and this record will act
187191
as the meta table for translating the object in the bucket to a downloadable
188192
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.
189197
"""
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

Comments
 (0)