Skip to content

Commit 42dc7ed

Browse files
committed
Fix #118 -- Support custom AWS locations without a trailing slash
1 parent 99c039f commit 42dc7ed

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

s3file/middleware.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import logging
3-
import os
3+
import pathlib
44

55
from s3file.storages import local_dev, storage
66

@@ -28,11 +28,10 @@ def __call__(self, request):
2828
def get_files_from_storage(paths):
2929
"""Return S3 file where the name does not include the path."""
3030
for path in paths:
31-
if storage.location:
32-
path = path.replace(os.path.dirname(storage.location) + "/", "", 1)
31+
path = pathlib.PurePosixPath(path)
3332
try:
34-
f = storage.open(path)
35-
f.name = os.path.basename(path)
33+
f = storage.open(path.relative_to(storage.location))
34+
f.name = path.name
3635
yield f
37-
except OSError:
36+
except (OSError, ValueError):
3837
logger.exception("File not found: %s", path)

tests/test_middleware.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from django.core.files.base import ContentFile
24
from django.core.files.uploadedfile import SimpleUploadedFile
35

@@ -11,7 +13,9 @@ def test_get_files_from_storage(self):
1113
name = storage.save(
1214
"tmp/s3file/test_get_files_from_storage", ContentFile(content)
1315
)
14-
files = S3FileMiddleware.get_files_from_storage([name])
16+
files = S3FileMiddleware.get_files_from_storage(
17+
[os.path.join(storage.location, name)]
18+
)
1519
file = next(files)
1620
assert file.read() == content
1721

0 commit comments

Comments
 (0)