Skip to content

Commit

Permalink
add MINIO_STORAGE_REGION setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Krystof Beuermann authored and thomasf committed May 31, 2023
1 parent 176c9cb commit 7782818
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The following settings are available:
`minio.example.org:9000` (note that there is no scheme)).

- `MINIO_STORAGE_ACCESS_KEY` and `MINIO_STORAGE_SECRET_KEY` (mandatory)

- `MINIO_STORAGE_REGION`: Allows you to specify the region. By setting this configuration option, an additional HTTP request to Minio for region checking can be prevented, resulting in improved performance and reduced latency for generating presigned URLs.

- `MINIO_STORAGE_USE_HTTPS`: whether to use TLS or not (default: `True`). This
affect both how how Django internally communicates with the Minio server AND
Expand Down
10 changes: 8 additions & 2 deletions minio_storage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,17 @@ def get_setting(name: str, default=_NoValue) -> T.Any:


def create_minio_client_from_settings(*, minio_kwargs=None):
minio_kwargs = minio_kwargs or {}
kwargs = {}
endpoint = get_setting("MINIO_STORAGE_ENDPOINT")
access_key = get_setting("MINIO_STORAGE_ACCESS_KEY")
secret_key = get_setting("MINIO_STORAGE_SECRET_KEY")
secure = get_setting("MINIO_STORAGE_USE_HTTPS", True)
region = get_setting("MINIO_STORAGE_REGION", None)
if region:
kwargs["region"] = region

if minio_kwargs:
kwargs.update(minio_kwargs)
# Making this client deconstructible allows it to be passed directly as
# an argument to MinioStorage, since Django needs to be able to
# deconstruct all Storage constructor arguments for Storages referenced in
Expand All @@ -378,7 +384,7 @@ def create_minio_client_from_settings(*, minio_kwargs=None):
access_key=access_key,
secret_key=secret_key,
secure=secure,
**minio_kwargs,
**kwargs,
)
return client

Expand Down
19 changes: 19 additions & 0 deletions tests/test_app/tests/settings_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.test import TestCase, override_settings

from minio_storage.storage import MinioMediaStorage
from tests.test_app.tests.utils import BaseTestMixin


class SettingsTests(BaseTestMixin, TestCase):
@override_settings(
MINIO_STORAGE_REGION="eu-central-666",
)
def test_settings_with_region(self):
ms = MinioMediaStorage()
region = ms.client._get_region(self.bucket_name("tests-media"), None)
self.assertEqual(region, "eu-central-666")

def test_settings_without_region(self):
ms = MinioMediaStorage()
region = ms.client._get_region(self.bucket_name("tests-media"), None)
self.assertEqual(region, "us-east-1")

0 comments on commit 7782818

Please sign in to comment.