-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmodels.py
More file actions
702 lines (606 loc) · 30.4 KB
/
models.py
File metadata and controls
702 lines (606 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
"""
django-minio-backend
A MinIO-compatible custom storage backend for Django
References:
* https://github.com/minio/minio-py
* https://docs.djangoproject.com/en/3.2/howto/custom-file-storage/
"""
import io
import json
import logging
import mimetypes
import ssl
import datetime
import sys
from pathlib import Path
from typing import Union, List, Tuple, Optional
from concurrent.futures import ThreadPoolExecutor, as_completed
import hashlib
from datetime import timedelta
# noinspection PyPackageRequirements MinIO_requirement
import certifi
import minio
import minio.datatypes
import minio.error
import minio.helpers
import urllib3
import urllib3.exceptions
from django.core.files import File
from django.core.files.storage import Storage, storages
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.utils.deconstruct import deconstructible
from django.core.cache import cache
from .utils import MinioServerStatus, PrivatePublicMixedError, ConfigurationError, get_setting
__all__ = ['MinioBackend', 'MinioBackendStatic', 'get_iso_date', 'iso_date_prefix', ]
logger = logging.getLogger(__name__)
def get_iso_date() -> str:
"""Get current date in ISO8601 format [year-month-day] (zero-padded) as string"""
now = datetime.datetime.now(datetime.UTC)
return now.strftime("%Y-%m-%d")
def iso_date_prefix(_, file_name_ext: str) -> str:
"""
Get filename prepended with current date in ISO8601 format [year-month-day] as string
The date prefix will be the folder's name storing the object e.g.: 2020-12-31/cat.png
"""
return f"{get_iso_date()}/{file_name_ext}"
class S3File(File):
"""A file returned from the Minio server"""
def __init__(self, file, name, storage):
super().__init__(file, name)
self._storage = storage
def open(self, mode=None, *args, **kwargs):
if self.closed:
self.file = self._storage.open(self.name, mode or "rb").file
return super().open(mode, *args, **kwargs)
@deconstructible
class MinioBackend(Storage):
"""
:param bucket_name (str): The bucket's name where file(s) will be stored
:arg *args: An arbitrary number of arguments. Stored in the self._META_ARGS class field
:arg **kwargs: An arbitrary number of key-value arguments.
Stored in the self._META_KWARGS class field
Through self._META_KWARGS, the "metadata", "sse" and "progress" fields can be set
for the underlying MinIO SDK's put_object() method
"""
DEFAULT_MEDIA_FILES_BUCKET = 'auto-generated-bucket-media-files' # PRIVATE BY DEFAULT
def __init__(self,
bucket_name: str = '',
storage_name: str = 'default',
*args,
**kwargs):
if not storages.backends[storage_name].get("OPTIONS"):
raise ConfigurationError("OPTIONS not present in STORAGES in settings.py")
# received kwargs are preferred. missing keys are filled from storages.backends
kwargs = {**kwargs, **{k: v for k, v in storages.backends[storage_name]["OPTIONS"].items() if k not in kwargs}}
# If bucket_name is not provided, MinioBackend falls back to using DEFAULT_MEDIA_FILES_BUCKET
# See https://docs.djangoproject.com/en/3.2/ref/settings/#default-file-storage
if not bucket_name or bucket_name == '':
self._BUCKET_NAME: str = kwargs.get("MINIO_DEFAULT_BUCKET", self.DEFAULT_MEDIA_FILES_BUCKET)
else:
self._BUCKET_NAME: str = bucket_name
self._META_ARGS = args
self._META_KWARGS = kwargs
self._REPLACE_EXISTING = kwargs.get('replace_existing', False)
self.__CLIENT: Union[minio.Minio, None] = None # This client is used for internal communication only. Communication this way should not leave the host network's perimeter
self.__CLIENT_EXT: Union[minio.Minio, None] = None # This client is used for external communication. This client is necessary for creating region-aware pre-signed URLs
self.__MINIO_ENDPOINT: str = kwargs.get("MINIO_ENDPOINT", "")
self.__MINIO_EXTERNAL_ENDPOINT: str = kwargs.get("MINIO_EXTERNAL_ENDPOINT", self.__MINIO_ENDPOINT)
self.__MINIO_ACCESS_KEY: str = kwargs.get("MINIO_ACCESS_KEY")
self.__MINIO_SECRET_KEY: str = kwargs.get("MINIO_SECRET_KEY")
self.__MINIO_USE_HTTPS: bool = kwargs.get("MINIO_USE_HTTPS")
self.__MINIO_REGION: str = kwargs.get("MINIO_REGION", "us-east-1") # MINIO defaults to "us-east-1" when region is set to None
self.__MINIO_EXTERNAL_ENDPOINT_USE_HTTPS: bool = kwargs.get("MINIO_EXTERNAL_ENDPOINT_USE_HTTPS", self.__MINIO_USE_HTTPS)
self.__MINIO_BUCKET_CHECK_ON_SAVE: bool = kwargs.get("MINIO_BUCKET_CHECK_ON_SAVE", False)
self.__MINIO_URL_EXPIRY_HOURS = kwargs.get("MINIO_URL_EXPIRY_HOURS", datetime.timedelta(days=7))
self.__BASE_URL = f'{"https://" if self.__MINIO_USE_HTTPS else "http://"}{self.__MINIO_ENDPOINT}'
self.__BASE_URL_EXTERNAL = f'{"https://" if self.__MINIO_EXTERNAL_ENDPOINT_USE_HTTPS else "http://"}{self.__MINIO_EXTERNAL_ENDPOINT}'
self.__SAME_ENDPOINTS = self.__MINIO_ENDPOINT == self.__MINIO_EXTERNAL_ENDPOINT
self.PRIVATE_BUCKETS: List[str] = kwargs.get("MINIO_PRIVATE_BUCKETS", [])
self.PUBLIC_BUCKETS: List[str] = kwargs.get("MINIO_PUBLIC_BUCKETS", [])
if self.bucket == self.DEFAULT_MEDIA_FILES_BUCKET:
self.PRIVATE_BUCKETS.append(self.DEFAULT_MEDIA_FILES_BUCKET)
if self.bucket not in [*self.PRIVATE_BUCKETS, *self.PUBLIC_BUCKETS]:
logger.warning(f'Bucket ({self.bucket}) is not declared either in MINIO_PRIVATE_BUCKETS or '
f'MINIO_PUBLIC_BUCKETS. Falling back to defaults treating bucket as PRIVATE.')
self.PRIVATE_BUCKETS.append(self.bucket)
# https://docs.min.io/docs/python-client-api-reference.html
http_client_from_kwargs = self._META_KWARGS.get("http_client", None)
http_client_from_settings = kwargs.get("MINIO_HTTP_CLIENT")
self.HTTP_CLIENT: urllib3.poolmanager.PoolManager = http_client_from_kwargs or http_client_from_settings
if bucket_name_intersection := list(set(self.PRIVATE_BUCKETS) & set(self.PUBLIC_BUCKETS)):
raise PrivatePublicMixedError(
f'One or more buckets have been declared both private and public: {bucket_name_intersection}'
)
# Backend extension that caches presigned URLs to avoid regenerating them
# on every request. The cache key is based on the file name and ETag.
self.url_caching_enabled = get_setting('MINIO_URL_CACHING_ENABLED', False)
self.url_cache_timeout = get_setting(
'MINIO_URL_CACHE_TIMEOUT',
int(get_setting('MINIO_URL_EXPIRY_HOURS', timedelta(days=1)).total_seconds() * 0.8)
)
# Cache prefix to avoid collisions
self.cache_prefix = get_setting('MINIO_URL_CACHE_PREFIX', 'minio_url_')
# Multipart upload configuration
self.__MINIO_MULTIPART_UPLOAD: bool = kwargs.get('MINIO_MULTIPART_UPLOAD', False)
self.__MINIO_MULTIPART_THRESHOLD: int = kwargs.get('MINIO_MULTIPART_THRESHOLD', 10 * 1024 * 1024) # 10MB default
self.__MINIO_MULTIPART_PART_SIZE: int = kwargs.get('MINIO_MULTIPART_PART_SIZE', 10 * 1024 * 1024) # 10MB default
self.validate_settings()
"""
django.core.files.storage.Storage
"""
def _save(self, file_path_name: str, content: InMemoryUploadedFile) -> str:
"""
Saves a file to Minio by implementing Minio.put_object()
:param file_path_name (str): Path to file + file name + file extension | i.e.: images/2018-12-31/cat.png
:param content (InMemoryUploadedFile): File object
:return:
"""
if self.__MINIO_BUCKET_CHECK_ON_SAVE:
# Create a bucket if not exists
self.check_bucket_existence()
# Check if an object with a name already exists; delete it if so
try:
if self._REPLACE_EXISTING and self.stat(file_path_name):
self.delete(file_path_name)
except (minio.error.S3Error, minio.error.ServerError, urllib3.exceptions.MaxRetryError, AttributeError):
pass
# Upload object
file_path: Path = Path(file_path_name) # app name + file.suffix
# Determine file size
content.seek(0, 2) # Seek to end
file_size = content.tell()
content.seek(0) # Reset to beginning
# Decide whether to use multipart upload
use_multipart = self.__MINIO_MULTIPART_UPLOAD and file_size >= self.__MINIO_MULTIPART_THRESHOLD
if use_multipart:
# Use multipart upload for large files
self.client.put_object(
bucket_name=self.bucket,
object_name=file_path.as_posix(),
data=content,
length=-1,
content_type=self._guess_content_type(file_path_name, content),
metadata=self._META_KWARGS.get('metadata', None),
sse=self._META_KWARGS.get('sse', None),
progress=self._META_KWARGS.get('progress', None),
part_size=self.__MINIO_MULTIPART_PART_SIZE,
num_parallel_uploads=self._META_KWARGS.get('num_parallel_uploads', 3),
tags=self._META_KWARGS.get('tags', None),
retention=self._META_KWARGS.get('retention', None),
legal_hold=self._META_KWARGS.get('legal_hold', False),
write_offset=self._META_KWARGS.get('write_offset', None),
)
else:
# Traditional upload - read entire file into memory
content_bytes: io.BytesIO = io.BytesIO(content.read())
content_length: int = len(content_bytes.getvalue())
self.client.put_object(
bucket_name=self.bucket,
object_name=file_path.as_posix(),
data=content_bytes,
length=content_length,
content_type=self._guess_content_type(file_path_name, content),
metadata=self._META_KWARGS.get('metadata', None),
sse=self._META_KWARGS.get('sse', None),
progress=self._META_KWARGS.get('progress', None),
part_size=0,
num_parallel_uploads=self._META_KWARGS.get('num_parallel_uploads', 3),
tags=self._META_KWARGS.get('tags', None),
retention=self._META_KWARGS.get('retention', None),
legal_hold=self._META_KWARGS.get('legal_hold', False),
write_offset=self._META_KWARGS.get('write_offset', None),
)
return file_path.as_posix()
def _get_cache_key(self, name: str) -> str:
"""
Generate a cache key based on the ETag.
For additional uniqueness, try to include the more unique metadata if available.
"""
try:
# Try to get the ETag from the object stats
obj = self.stat(name)
etag = obj.etag if hasattr(obj, 'etag') else ''
except (AttributeError, Exception):
etag = ''
# Create a unique key using the bucket name, file name, and ETag
key_parts = [self.bucket, name, etag]
key_string = '_'.join(key_parts)
# Create a hash of the key to ensure it's not too long for the cache
key_hash = hashlib.md5(key_string.encode()).hexdigest()
return f"{self.cache_prefix}{key_hash}"
def get_available_name(self, name, max_length=None):
"""
Return a filename that's free on the target storage system and
available for new content to be written to.
"""
if self._REPLACE_EXISTING:
return name
return super(MinioBackend, self).get_available_name(name, max_length)
def _open(self, object_name, mode='rb', **kwargs) -> S3File:
"""
Implements the Storage._open(name,mode='rb') method
:param name (str): object_name [path to file excluding bucket name which is implied]
:kwargs (dict): passed on to the underlying MinIO client's get_object() method
"""
if mode != 'rb':
raise ValueError('Files retrieved from MinIO are read-only. Use save() method to override contents')
resp: urllib3.response.HTTPResponse = self.client.get_object(bucket_name=self.bucket, object_name=object_name, **kwargs)
try:
file = S3File(file=io.BytesIO(resp.read()), name=object_name, storage=self)
return file
finally:
resp.close()
resp.release_conn()
def stat(self, name: str) -> minio.datatypes.Object:
"""
Get object information and metadata of an object
:raises minio.error.S3Error: If the object doesn't exist
:raises minio.error.ServerError: If the minio backend couldn't be reached
:raises urllib3.exceptions.MaxRetryError: If the minio backend request has timed out
"""
return self.client.stat_object(bucket_name=self.bucket, object_name=Path(name).as_posix())
def delete(self, name: str):
"""
Deletes an object in Django and MinIO.
This method is called only when an object is deleted from its own `change view` i.e.:
http://django.test/admin/upload/privateattachment/13/change/
This method is NOT called during a bulk_delete order!
:param name: File object name
"""
object_name = Path(name).as_posix()
self.client.remove_object(bucket_name=self.bucket, object_name=object_name)
def exists(self, name: str) -> bool:
"""Check if an object with the name already exists"""
object_name = Path(name).as_posix()
try:
self.stat(object_name)
except minio.error.S3Error as e:
if ("collectstatic" in sys.argv) and getattr(e, "code", None) in {"NoSuchKey", "NoSuchObject"}:
return False
logger.info(msg=f"Object could not be found: {self.bucket}/{name}", exc_info=e)
return False
except (minio.error.ServerError, urllib3.exceptions.MaxRetryError, AttributeError, FileNotFoundError) as e:
logger.info(msg=f"Object could not be found: {self.bucket}/{name}", exc_info=e)
return False
return True
def listdir(self, path: str = '', **kwargs) -> Tuple[List[str], List[str]]:
"""
List the contents of the specified path in the configured bucket.
Return a 2-tuple of lists: (directories, files).
This method sets the `recursive` kwarg to True by default.
The `prefix` key in kwargs will be ignored. Provide it through the `path` arg.
:kwargs: Passed on to the underlying MinIO client's list_objects() method
"""
prefix = path.strip('/')
if prefix:
prefix += '/'
else:
prefix = ''
prefix_len = len(prefix)
# Always recursive to find everything under the prefix
kwargs.setdefault('recursive', True)
dirs = set()
files = []
objects = self.client.list_objects(bucket_name=self.bucket, prefix=prefix, **kwargs)
for obj in objects:
relpath = obj.object_name[prefix_len:]
if not relpath:
continue
if '/' in relpath:
subdir = relpath.split('/', 1)[0]
dirs.add(subdir)
else:
files.append(relpath)
return sorted(dirs), sorted(files)
def size(self, name: str) -> int:
"""Get an object's size"""
try:
obj = self.stat(Path(name).as_posix())
except (minio.error.S3Error, minio.error.ServerError, urllib3.exceptions.MaxRetryError, AttributeError, FileNotFoundError):
return 0
return obj.size
def url(self, name: str):
"""
Returns url to object.
If the bucket is public, a direct link is provided.
If the bucket is private, a pre-signed link is provided.
If bucket is public, direct link is provided.
if bucket is private, a pre-signed link is provided.
This method does not use caching by default.
Set MINIO_URL_CACHING_ENABLED to True in settings.py to enable caching.
:param name: (str) file path + file name + suffix
:return: (str) URL to object
"""
if self.is_bucket_public or not self.url_caching_enabled:
return self._generate_url(name)
# For private buckets with caching enabled, check cache first
cache_key = self._get_cache_key(name)
if cached_url := cache.get(cache_key):
return cached_url
# Cache miss, generate new URL
if (generated_url := self._generate_url(name)) is None:
return None
# Cache the URL
cache.set(cache_key, generated_url, self.url_cache_timeout)
return generated_url
def _generate_url(self, name: str):
"""
Original URL generation implementation for multiple uses.
:param name: (str) file path + file name + suffix
:return: (str) URL to object
"""
client = self.client if self.same_endpoints else self.client_external
if self.is_bucket_public:
# noinspection PyProtectedMember
base_url = client._base_url.build(method="GET", region=self.__MINIO_REGION).geturl()
return f'{base_url}{self.bucket}/{name}'
# private bucket
try:
u: str = client.presigned_get_object(
bucket_name=self.bucket,
object_name=name,
expires=self.__MINIO_URL_EXPIRY_HOURS, # Default is 7 days
)
return u
except urllib3.exceptions.MaxRetryError:
raise ConnectionError("Couldn't connect to Minio. Check django_minio_backend parameters in Django-Settings")
def path(self, name):
"""The MinIO storage system doesn't support absolute paths"""
raise NotImplementedError("The MinIO storage system doesn't support absolute paths.")
def get_accessed_time(self, name: str) -> datetime.datetime:
"""
Return the last accessed time (as a datetime) of the file specified by
name. The datetime will be timezone-aware if USE_TZ=True.
"""
raise NotImplementedError('MinIO does not store last accessed time')
def get_created_time(self, name: str) -> datetime.datetime:
"""
Return the creation time (as a datetime) of the file specified by name.
The datetime will be timezone-aware if USE_TZ=True.
"""
raise NotImplementedError('MinIO does not store creation time')
def get_modified_time(self, name: str) -> datetime.datetime:
"""
Return the last modified time (as a datetime) of the file specified by
name. The datetime will be timezone-aware if USE_TZ=True.
"""
if get_setting("USE_TZ"):
return self.stat(name).last_modified
return self.stat(name).last_modified.replace(tzinfo=None) # remove timezone info
@staticmethod
def _guess_content_type(file_path_name: str, content: InMemoryUploadedFile):
if hasattr(content, 'content_type'):
return content.content_type
guess = mimetypes.guess_type(file_path_name)[0]
if guess is None:
return 'application/octet-stream' # default
return guess
"""
MinioBackend
"""
@property
def same_endpoints(self) -> bool:
"""
Returns True if (self.__MINIO_ENDPOINT == self.__MINIO_EXTERNAL_ENDPOINT)
"""
return self.__SAME_ENDPOINTS
@property
def bucket(self) -> str:
"""Get the configured bucket's [self.bucket] name"""
return self._BUCKET_NAME
@property
def is_bucket_public(self) -> bool:
"""Check if configured bucket [self.bucket] is public"""
return True if self.bucket in self.PUBLIC_BUCKETS else False
def is_minio_available(self) -> MinioServerStatus:
"""Check if configured MinIO server is available"""
if not self.__MINIO_ENDPOINT:
mss = MinioServerStatus(None)
mss.add_message('MINIO_ENDPOINT is not configured in Django settings')
return mss
with urllib3.PoolManager(cert_reqs=ssl.CERT_REQUIRED, ca_certs=certifi.where()) as http:
try:
r = http.request('GET', f'{self.__BASE_URL}/minio/index.html')
return MinioServerStatus(r)
except urllib3.exceptions.MaxRetryError as e:
mss = MinioServerStatus(None)
mss.add_message(f'Could not open connection to {self.__BASE_URL}/minio/index.html\n'
f'Reason: {e}')
return mss
except Exception as e:
mss = MinioServerStatus(None)
mss.add_message(repr(e))
return mss
@property
def client(self) -> minio.Minio:
"""
Get handle to an (already) instantiated minio.Minio instance. This is the default Client.
If "MINIO_EXTERNAL_ENDPOINT" != MINIO_ENDPOINT, this client is used for internal communication only
"""
return self.__CLIENT or self._create_new_client()
@property
def client_external(self) -> minio.Minio:
"""Get handle to an (already) instantiated EXTERNAL minio.Minio instance for generating pre-signed URLs for external access"""
return self.__CLIENT_EXT or self._create_new_client(external=True)
@property
def base_url(self) -> str:
"""Get internal base URL to MinIO"""
return self.__BASE_URL
@property
def base_url_external(self) -> str:
"""Get external base URL to MinIO"""
return self.__BASE_URL_EXTERNAL
def _create_new_client(self, external: bool = False) -> minio.Minio:
"""
Instantiates a new Minio client and assigns it to their respective class variable
:param external: If True, the returned value is self.__CLIENT_EXT instead of self.__CLIENT
"""
if external:
if not self.__CLIENT_EXT:
self.__CLIENT_EXT = minio.Minio(
endpoint=self.__MINIO_EXTERNAL_ENDPOINT,
access_key=self.__MINIO_ACCESS_KEY,
secret_key=self.__MINIO_SECRET_KEY,
secure=self.__MINIO_EXTERNAL_ENDPOINT_USE_HTTPS,
http_client=self.HTTP_CLIENT,
region=self.__MINIO_REGION,
)
return self.__CLIENT_EXT
else:
if not self.__CLIENT:
self.__CLIENT = minio.Minio(
endpoint=self.__MINIO_ENDPOINT,
access_key=self.__MINIO_ACCESS_KEY,
secret_key=self.__MINIO_SECRET_KEY,
secure=self.__MINIO_USE_HTTPS,
http_client=self.HTTP_CLIENT,
region=self.__MINIO_REGION,
)
return self.__CLIENT
# MAINTENANCE
def check_bucket_existence(self):
"""Check if configured bucket [self.bucket] exists"""
if not self.client.bucket_exists(bucket_name=self.bucket):
self.client.make_bucket(bucket_name=self.bucket)
def check_bucket_existences(self, max_workers: Optional[int] = None):
"""
Check if all buckets configured in settings.py exist. Create them if they don't.
Args:
max_workers: Maximum number of concurrent workers.
Defaults to min(bucket_count, 10)
Raises:
Exception: If any bucket check or creation fails
"""
all_buckets = [*self.PUBLIC_BUCKETS, *self.PRIVATE_BUCKETS]
if not all_buckets:
logger.warning("No buckets configured")
return
# Calculate optimal worker count
if max_workers is None:
max_workers = min(len(all_buckets), 10)
def check_and_create_bucket(bucket: str) -> Tuple[str, bool, Optional[Exception]]:
"""
Check and create a single bucket.
Returns:
Tuple of (bucket_name, already_existed, error)
"""
try:
exists = self.client.bucket_exists(bucket_name=bucket)
if not exists:
self.client.make_bucket(bucket_name=bucket)
logger.info(f"Created bucket: {bucket}")
return bucket, False, None
else:
logger.debug(f"Bucket already exists: {bucket}")
return bucket, True, None
except Exception as exc:
logger.error(f"Error checking/creating bucket {bucket}: {exc}", exc_info=True)
return bucket, False, exc
errors = []
created_count = 0
existing_count = 0
logger.info(f"Checking {len(all_buckets)} buckets with {max_workers} workers")
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit all bucket checks
future_to_bucket = {
executor.submit(check_and_create_bucket, bucket): bucket
for bucket in all_buckets
}
# Process results as they complete
for future in as_completed(future_to_bucket):
bucket_name = future_to_bucket[future]
try:
bucket, existed, error = future.result()
if error:
errors.append((bucket, error))
elif existed:
existing_count += 1
else:
created_count += 1
except Exception as e:
# Catch any unexpected errors from the future itself
logger.error(f"Unexpected error processing bucket {bucket_name}: {e}")
errors.append((bucket_name, e))
# Summary logging
logger.info(
f"Bucket check complete: {existing_count} existed, "
f"{created_count} created, {len(errors)} errors"
)
# Raise if any errors occurred
if errors:
error_msg = "\n".join([f" - {bucket}: {error}" for bucket, error in errors])
raise Exception(f"Failed to check/create {len(errors)} bucket(s):\n{error_msg}")
def set_bucket_policy(self, bucket: str, policy: dict):
"""Set a custom bucket policy"""
self.client.set_bucket_policy(bucket_name=bucket, policy=json.dumps(policy))
def set_bucket_to_public(self):
"""Set bucket policy to be public. It can be then accessed via public URLs"""
policy_public_read_only = {"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": "s3:GetBucketLocation",
"Resource": f"arn:aws:s3:::{self.bucket}"
},
{
"Sid": "",
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": "s3:ListBucket",
"Resource": f"arn:aws:s3:::{self.bucket}"
},
{
"Sid": "",
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": "s3:GetObject",
"Resource": f"arn:aws:s3:::{self.bucket}/*"
}
]}
self.set_bucket_policy(self.bucket, policy_public_read_only)
def validate_settings(self):
"""
validate_settings raises a ConfigurationError exception when one of the following conditions is met:
* Neither MINIO_PRIVATE_BUCKETS nor MINIO_PUBLIC_BUCKETS have been declared and configured with at least 1 bucket
* A mandatory parameter (MINIO_ENDPOINT, MINIO_ACCESS_KEY, MINIO_SECRET_KEY or MINIO_USE_HTTPS) hasn't been declared and configured properly
"""
# minimum 1 bucket has to be declared
if not (self.PRIVATE_BUCKETS or self.PUBLIC_BUCKETS):
raise ConfigurationError(
'Either '
'MINIO_PRIVATE_BUCKETS'
' or '
'MINIO_PUBLIC_BUCKETS '
'must be configured in your settings.py (can be both)'
)
# mandatory parameters must be configured
mandatory_parameters = (self.__MINIO_ENDPOINT, self.__MINIO_ACCESS_KEY, self.__MINIO_SECRET_KEY)
if not all(mandatory_parameters):
raise ConfigurationError(
"A mandatory parameter (MINIO_ENDPOINT, MINIO_ACCESS_KEY, MINIO_SECRET_KEY or MINIO_USE_HTTPS) hasn't been configured properly"
)
@deconstructible
class MinioBackendStatic(MinioBackend):
"""
MinIO-compatible Django custom storage system for Django static files.
The used bucket can be configured in settings.py through `STORAGES.staticfiles.BACKEND`
:arg *args: Should not be used for static files. It's here for compatibility only
:arg **kwargs: Should not be used for static files. It's here for compatibility only
"""
DEFAULT_STATIC_FILES_BUCKET = 'auto-generated-bucket-static-files'
def __init__(self, *args, **kwargs):
static_files_bucket = kwargs.get("MINIO_STATIC_FILES_BUCKET", self.DEFAULT_STATIC_FILES_BUCKET)
logger.debug(f"MinioBackendStatic.static_files_bucket = {static_files_bucket}")
kwargs["MINIO_PUBLIC_BUCKETS"] = [static_files_bucket, ] # hardcoded. static files must be public
super().__init__(bucket_name=static_files_bucket, storage_name="staticfiles", *args, **kwargs)
def path(self, name):
"""The MinIO storage system doesn't support absolute paths"""
raise NotImplementedError("The MinIO storage system doesn't support absolute paths.")
def get_accessed_time(self, name: str):
"""MinIO does not store last accessed time"""
raise NotImplementedError('MinIO does not store last accessed time')
def get_created_time(self, name: str):
"""MinIO does not store creation time"""
raise NotImplementedError('MinIO does not store creation time')