Skip to content

Commit e278b09

Browse files
committed
Fix #93 -- Always use POSIX style S3 keys
1 parent cc3239e commit e278b09

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

s3file/forms.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
import os
2+
import pathlib
33
import uuid
44

55
from django.conf import settings
@@ -13,7 +13,7 @@ class S3FileInputMixin:
1313
"""FileInput that uses JavaScript to directly upload to Amazon S3."""
1414

1515
needs_multipart_form = False
16-
upload_path = getattr(settings, 'S3FILE_UPLOAD_PATH', os.path.join('tmp', 's3file'))
16+
upload_path = getattr(settings, 'S3FILE_UPLOAD_PATH', pathlib.PurePosixPath('tmp', 's3file'))
1717
expires = settings.SESSION_COOKIE_AGE
1818

1919
@property
@@ -29,7 +29,7 @@ def build_attrs(self, *args, **kwargs):
2929

3030
accept = attrs.get('accept')
3131
response = self.client.generate_presigned_post(
32-
self.bucket_name, os.path.join(self.upload_folder, '${filename}'),
32+
self.bucket_name, str(pathlib.PurePosixPath(self.upload_folder, '${filename}')),
3333
Conditions=self.get_conditions(accept),
3434
ExpiresIn=self.expires,
3535
)
@@ -50,7 +50,7 @@ def build_attrs(self, *args, **kwargs):
5050
def get_conditions(self, accept):
5151
conditions = [
5252
{"bucket": self.bucket_name},
53-
["starts-with", "$key", self.upload_folder],
53+
["starts-with", "$key", str(self.upload_folder)],
5454
{"success_action_status": "201"},
5555
]
5656
if accept and ',' not in accept:
@@ -66,10 +66,10 @@ def get_conditions(self, accept):
6666

6767
@cached_property
6868
def upload_folder(self):
69-
return os.path.join(
69+
return str(pathlib.PurePosixPath(
7070
self.upload_path,
7171
uuid.uuid4().hex,
72-
)
72+
)) # S3 uses POSIX paths
7373

7474
class Media:
7575
js = (

tests/test_forms.py

+4
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,7 @@ def test_file_insert_submit_value(self, driver, live_server, upload_file, freeze
165165

166166
def test_media(self):
167167
assert ClearableFileInput().media._js == ['s3file/js/s3file.js']
168+
169+
def test_upload_folder(self):
170+
assert ClearableFileInput().upload_folder.startswith('tmp/s3file/')
171+
assert len(ClearableFileInput().upload_folder) == 43

0 commit comments

Comments
 (0)