Skip to content

Commit 081d5e1

Browse files
committed
Pull in changes from py-pa/django-minio-storage#144
1 parent af15c92 commit 081d5e1

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

isic/core/storages/minio.py

+71-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import boto3
44
from botocore.exceptions import ClientError
5+
from django.core.files import File
6+
import minio
57
from minio_storage.files import ReadOnlySpooledTemporaryFile
6-
from minio_storage.storage import MinioMediaStorage
8+
from minio_storage.policy import Policy
9+
from minio_storage.storage import MinioStorage, create_minio_client_from_settings, get_setting
710

811

912
class StringableReadOnlySpooledTemporaryFile(ReadOnlySpooledTemporaryFile):
@@ -24,11 +27,75 @@ def readline(self, *args, **kwargs):
2427
return super().readline(*args, **kwargs).decode()
2528

2629

27-
class StringableMinioMediaStorage(MinioMediaStorage):
30+
class FixedMinioMediaStorage(MinioStorage):
31+
# From https://github.com/py-pa/django-minio-storage/pull/139
2832
file_class = StringableReadOnlySpooledTemporaryFile
2933

30-
31-
class MinioS3ProxyStorage(StringableMinioMediaStorage):
34+
# From https://github.com/py-pa/django-minio-storage/pull/144
35+
def __init__( # noqa: C901
36+
self,
37+
*,
38+
minio_client: minio.Minio | None = None,
39+
bucket_name: str | None = None,
40+
base_url: str | None = None,
41+
file_class: type[File] | None = None,
42+
auto_create_bucket: bool | None = None,
43+
presign_urls: bool | None = None,
44+
auto_create_policy: bool | None = None,
45+
policy_type: Policy | None = None,
46+
object_metadata: dict[str, str] | None = None,
47+
backup_format: str | None = None,
48+
backup_bucket: str | None = None,
49+
assume_bucket_exists: bool | None = None,
50+
):
51+
if minio_client is None:
52+
minio_client = create_minio_client_from_settings()
53+
if bucket_name is None:
54+
bucket_name = get_setting("MINIO_STORAGE_MEDIA_BUCKET_NAME")
55+
if base_url is None:
56+
base_url = get_setting("MINIO_STORAGE_MEDIA_URL", None)
57+
if auto_create_bucket is None:
58+
auto_create_bucket = get_setting("MINIO_STORAGE_AUTO_CREATE_MEDIA_BUCKET", False)
59+
if presign_urls is None:
60+
presign_urls = get_setting("MINIO_STORAGE_MEDIA_USE_PRESIGNED", False)
61+
auto_create_policy_setting = get_setting(
62+
"MINIO_STORAGE_AUTO_CREATE_MEDIA_POLICY", "GET_ONLY"
63+
)
64+
if auto_create_policy is None:
65+
auto_create_policy = (
66+
True if isinstance(auto_create_policy_setting, str) else auto_create_policy_setting
67+
)
68+
if policy_type is None:
69+
policy_type = (
70+
Policy(auto_create_policy_setting)
71+
if isinstance(auto_create_policy_setting, str)
72+
else Policy.get
73+
)
74+
if object_metadata is None:
75+
object_metadata = get_setting("MINIO_STORAGE_MEDIA_OBJECT_METADATA", None)
76+
if backup_format is None:
77+
backup_format = get_setting("MINIO_STORAGE_MEDIA_BACKUP_FORMAT", None)
78+
if backup_bucket is None:
79+
backup_bucket = get_setting("MINIO_STORAGE_MEDIA_BACKUP_BUCKET", None)
80+
if assume_bucket_exists is None:
81+
assume_bucket_exists = get_setting("MINIO_STORAGE_ASSUME_MEDIA_BUCKET_EXISTS", False)
82+
super().__init__(
83+
minio_client,
84+
bucket_name,
85+
base_url=base_url,
86+
file_class=file_class,
87+
auto_create_bucket=auto_create_bucket,
88+
presign_urls=presign_urls,
89+
auto_create_policy=auto_create_policy,
90+
policy_type=policy_type,
91+
object_metadata=object_metadata,
92+
backup_format=backup_format,
93+
backup_bucket=backup_bucket,
94+
assume_bucket_exists=assume_bucket_exists,
95+
)
96+
97+
98+
class MinioS3ProxyStorage(FixedMinioMediaStorage):
3299
"""
33100
A storage backend that proxies to S3 if the file doesn't exist in Minio.
34101

isic/settings/development.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
MINIO_STORAGE_AUTO_CREATE_MEDIA_POLICY = "READ_WRITE"
8787
MINIO_STORAGE_MEDIA_USE_PRESIGNED = True
8888

89-
STORAGES["default"]["BACKEND"] = "isic.core.storages.minio.StringableMinioMediaStorage" # noqa: F405
89+
STORAGES["default"]["BACKEND"] = "isic.core.storages.minio.FixedMinioMediaStorage" # noqa: F405
9090

9191
ISIC_PLACEHOLDER_IMAGES = True
9292
# Use the MinioS3ProxyStorage for local development with ISIC_PLACEHOLDER_IMAGES

isic/settings/testing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
ZIP_DOWNLOAD_BASIC_AUTH_TOKEN = "insecuretestzipdownloadauthtoken" # noqa: S105
4040
ZIP_DOWNLOAD_WILDCARD_URLS = False
4141

42-
STORAGES["default"] = {"BACKEND": "isic.core.storages.minio.StringableMinioMediaStorage"} # noqa: F405
42+
STORAGES["default"] = {"BACKEND": "isic.core.storages.minio.FixedMinioMediaStorage"} # noqa: F405
4343

4444

4545
MINIO_STORAGE_ENDPOINT = os.environ["DJANGO_MINIO_STORAGE_ENDPOINT"]

0 commit comments

Comments
 (0)