|
9 | 9 | import minio.error as merr
|
10 | 10 | from django.conf import settings
|
11 | 11 | from django.core.exceptions import ImproperlyConfigured
|
| 12 | +from django.core.files.base import File |
12 | 13 | from django.core.files.storage import Storage
|
13 | 14 | from django.utils import timezone
|
14 | 15 | from django.utils.deconstruct import deconstructible
|
@@ -38,7 +39,7 @@ def __init__(
|
38 | 39 | bucket_name: str,
|
39 | 40 | *,
|
40 | 41 | base_url: T.Optional[str] = None,
|
41 |
| - file_class=None, |
| 42 | + file_class: T.Optional[T.Type[File]] = None, |
42 | 43 | auto_create_bucket: bool = False,
|
43 | 44 | presign_urls: bool = False,
|
44 | 45 | auto_create_policy: bool = False,
|
@@ -388,82 +389,134 @@ def create_minio_client_from_settings(*, minio_kwargs=None):
|
388 | 389 |
|
389 | 390 | @deconstructible
|
390 | 391 | class MinioMediaStorage(MinioStorage):
|
391 |
| - def __init__(self): |
392 |
| - client = create_minio_client_from_settings() |
393 |
| - bucket_name = get_setting("MINIO_STORAGE_MEDIA_BUCKET_NAME") |
394 |
| - base_url = get_setting("MINIO_STORAGE_MEDIA_URL", None) |
395 |
| - auto_create_bucket = get_setting( |
396 |
| - "MINIO_STORAGE_AUTO_CREATE_MEDIA_BUCKET", False |
397 |
| - ) |
398 |
| - auto_create_policy = get_setting( |
| 392 | + def __init__( # noqa: C901 |
| 393 | + self, |
| 394 | + *, |
| 395 | + minio_client: T.Optional[minio.Minio] = None, |
| 396 | + bucket_name: T.Optional[str] = None, |
| 397 | + base_url: T.Optional[str] = None, |
| 398 | + file_class: T.Optional[T.Type[File]] = None, |
| 399 | + auto_create_bucket: T.Optional[bool] = None, |
| 400 | + presign_urls: T.Optional[bool] = None, |
| 401 | + auto_create_policy: T.Optional[bool] = None, |
| 402 | + policy_type: T.Optional[Policy] = None, |
| 403 | + object_metadata: T.Optional[T.Dict[str, str]] = None, |
| 404 | + backup_format: T.Optional[str] = None, |
| 405 | + backup_bucket: T.Optional[str] = None, |
| 406 | + assume_bucket_exists: T.Optional[bool] = None, |
| 407 | + ): |
| 408 | + if minio_client is None: |
| 409 | + minio_client = create_minio_client_from_settings() |
| 410 | + if bucket_name is None: |
| 411 | + bucket_name = get_setting("MINIO_STORAGE_MEDIA_BUCKET_NAME") |
| 412 | + if base_url is None: |
| 413 | + base_url = get_setting("MINIO_STORAGE_MEDIA_URL", None) |
| 414 | + if auto_create_bucket is None: |
| 415 | + auto_create_bucket = get_setting( |
| 416 | + "MINIO_STORAGE_AUTO_CREATE_MEDIA_BUCKET", False |
| 417 | + ) |
| 418 | + if presign_urls is None: |
| 419 | + presign_urls = get_setting("MINIO_STORAGE_MEDIA_USE_PRESIGNED", False) |
| 420 | + auto_create_policy_setting = get_setting( |
399 | 421 | "MINIO_STORAGE_AUTO_CREATE_MEDIA_POLICY", "GET_ONLY"
|
400 | 422 | )
|
401 |
| - |
402 |
| - policy_type = Policy.get |
403 |
| - if isinstance(auto_create_policy, str): |
404 |
| - policy_type = Policy(auto_create_policy) |
405 |
| - auto_create_policy = True |
406 |
| - |
407 |
| - presign_urls = get_setting("MINIO_STORAGE_MEDIA_USE_PRESIGNED", False) |
408 |
| - backup_format = get_setting("MINIO_STORAGE_MEDIA_BACKUP_FORMAT", False) |
409 |
| - backup_bucket = get_setting("MINIO_STORAGE_MEDIA_BACKUP_BUCKET", False) |
410 |
| - |
411 |
| - assume_bucket_exists = get_setting( |
412 |
| - "MINIO_STORAGE_ASSUME_MEDIA_BUCKET_EXISTS", False |
413 |
| - ) |
414 |
| - |
415 |
| - object_metadata = get_setting("MINIO_STORAGE_MEDIA_OBJECT_METADATA", None) |
416 |
| - # print("SETTING", object_metadata) |
417 |
| - |
| 423 | + if auto_create_policy is None: |
| 424 | + auto_create_policy = ( |
| 425 | + True |
| 426 | + if isinstance(auto_create_policy_setting, str) |
| 427 | + else auto_create_policy_setting |
| 428 | + ) |
| 429 | + if policy_type is None: |
| 430 | + policy_type = ( |
| 431 | + Policy(auto_create_policy_setting) |
| 432 | + if isinstance(auto_create_policy_setting, str) |
| 433 | + else Policy.get |
| 434 | + ) |
| 435 | + if object_metadata is None: |
| 436 | + object_metadata = get_setting("MINIO_STORAGE_MEDIA_OBJECT_METADATA", None) |
| 437 | + if backup_format is None: |
| 438 | + backup_format = get_setting("MINIO_STORAGE_MEDIA_BACKUP_FORMAT", None) |
| 439 | + if backup_bucket is None: |
| 440 | + backup_bucket = get_setting("MINIO_STORAGE_MEDIA_BACKUP_BUCKET", None) |
| 441 | + if assume_bucket_exists is None: |
| 442 | + assume_bucket_exists = get_setting( |
| 443 | + "MINIO_STORAGE_ASSUME_MEDIA_BUCKET_EXISTS", False |
| 444 | + ) |
418 | 445 | super().__init__(
|
419 |
| - client, |
| 446 | + minio_client, |
420 | 447 | bucket_name,
|
| 448 | + base_url=base_url, |
| 449 | + file_class=file_class, |
421 | 450 | auto_create_bucket=auto_create_bucket,
|
| 451 | + presign_urls=presign_urls, |
422 | 452 | auto_create_policy=auto_create_policy,
|
423 | 453 | policy_type=policy_type,
|
424 |
| - base_url=base_url, |
425 |
| - presign_urls=presign_urls, |
| 454 | + object_metadata=object_metadata, |
426 | 455 | backup_format=backup_format,
|
427 | 456 | backup_bucket=backup_bucket,
|
428 | 457 | assume_bucket_exists=assume_bucket_exists,
|
429 |
| - object_metadata=object_metadata, |
430 | 458 | )
|
431 | 459 |
|
432 | 460 |
|
433 | 461 | @deconstructible
|
434 | 462 | class MinioStaticStorage(MinioStorage):
|
435 |
| - def __init__(self): |
436 |
| - client = create_minio_client_from_settings() |
437 |
| - base_url = get_setting("MINIO_STORAGE_STATIC_URL", None) |
438 |
| - bucket_name = get_setting("MINIO_STORAGE_STATIC_BUCKET_NAME") |
439 |
| - auto_create_bucket = get_setting( |
440 |
| - "MINIO_STORAGE_AUTO_CREATE_STATIC_BUCKET", False |
441 |
| - ) |
442 |
| - auto_create_policy = get_setting( |
| 463 | + def __init__( |
| 464 | + self, |
| 465 | + *, |
| 466 | + minio_client: T.Optional[minio.Minio] = None, |
| 467 | + bucket_name: T.Optional[str] = None, |
| 468 | + base_url: T.Optional[str] = None, |
| 469 | + file_class: T.Optional[T.Type[File]] = None, |
| 470 | + auto_create_bucket: T.Optional[bool] = None, |
| 471 | + presign_urls: T.Optional[bool] = None, |
| 472 | + auto_create_policy: T.Optional[bool] = None, |
| 473 | + policy_type: T.Optional[Policy] = None, |
| 474 | + object_metadata: T.Optional[T.Dict[str, str]] = None, |
| 475 | + assume_bucket_exists: T.Optional[bool] = None, |
| 476 | + ): |
| 477 | + if minio_client is None: |
| 478 | + minio_client = create_minio_client_from_settings() |
| 479 | + if bucket_name is None: |
| 480 | + bucket_name = get_setting("MINIO_STORAGE_STATIC_BUCKET_NAME") |
| 481 | + if base_url is None: |
| 482 | + base_url = get_setting("MINIO_STORAGE_STATIC_URL", None) |
| 483 | + if auto_create_bucket is None: |
| 484 | + auto_create_bucket = get_setting( |
| 485 | + "MINIO_STORAGE_AUTO_CREATE_STATIC_BUCKET", False |
| 486 | + ) |
| 487 | + if presign_urls is None: |
| 488 | + presign_urls = get_setting("MINIO_STORAGE_STATIC_USE_PRESIGNED", False) |
| 489 | + auto_create_policy_setting = get_setting( |
443 | 490 | "MINIO_STORAGE_AUTO_CREATE_STATIC_POLICY", "GET_ONLY"
|
444 | 491 | )
|
445 |
| - |
446 |
| - policy_type = Policy.get |
447 |
| - if isinstance(auto_create_policy, str): |
448 |
| - policy_type = Policy(auto_create_policy) |
449 |
| - auto_create_policy = True |
450 |
| - |
451 |
| - presign_urls = get_setting("MINIO_STORAGE_STATIC_USE_PRESIGNED", False) |
452 |
| - |
453 |
| - assume_bucket_exists = get_setting( |
454 |
| - "MINIO_STORAGE_ASSUME_STATIC_BUCKET_EXISTS", False |
455 |
| - ) |
456 |
| - |
457 |
| - object_metadata = get_setting("MINIO_STORAGE_STATIC_OBJECT_METADATA", None) |
458 |
| - |
| 492 | + if auto_create_policy is None: |
| 493 | + auto_create_policy = ( |
| 494 | + True |
| 495 | + if isinstance(auto_create_policy_setting, str) |
| 496 | + else auto_create_policy_setting |
| 497 | + ) |
| 498 | + if policy_type is None: |
| 499 | + policy_type = ( |
| 500 | + Policy(auto_create_policy_setting) |
| 501 | + if isinstance(auto_create_policy_setting, str) |
| 502 | + else Policy.get |
| 503 | + ) |
| 504 | + if object_metadata is None: |
| 505 | + object_metadata = get_setting("MINIO_STORAGE_STATIC_OBJECT_METADATA", None) |
| 506 | + if assume_bucket_exists is None: |
| 507 | + assume_bucket_exists = get_setting( |
| 508 | + "MINIO_STORAGE_ASSUME_STATIC_BUCKET_EXISTS", False |
| 509 | + ) |
459 | 510 | super().__init__(
|
460 |
| - client, |
| 511 | + minio_client, |
461 | 512 | bucket_name,
|
| 513 | + base_url=base_url, |
| 514 | + file_class=file_class, |
462 | 515 | auto_create_bucket=auto_create_bucket,
|
| 516 | + presign_urls=presign_urls, |
463 | 517 | auto_create_policy=auto_create_policy,
|
464 | 518 | policy_type=policy_type,
|
465 |
| - base_url=base_url, |
466 |
| - presign_urls=presign_urls, |
467 |
| - assume_bucket_exists=assume_bucket_exists, |
468 | 519 | object_metadata=object_metadata,
|
| 520 | + # backup_format and backup_bucket are not valid for static storage |
| 521 | + assume_bucket_exists=assume_bucket_exists, |
469 | 522 | )
|
0 commit comments