Skip to content

Commit

Permalink
Adding external endpoint configuration (#23)
Browse files Browse the repository at this point in the history
  - Added better support for container environments
  - Host URL used for file access URL generation is now configurable
  - New configuration parameter: MINIO_EXTERNAL_ENDPOINT
  - New configuration parameter: MINIO_EXTERNAL_ENDPOINT_USE_HTTPS
  - New MinioBackend property: same_endpoints
  - New MinioBackend property: base_url_external
  - Revert "Create stale.yml" (This reverts commit 5bc2105)
  • Loading branch information
theriverman committed Jun 4, 2021
1 parent b201e7b commit 418fc13
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/stale.yml

This file was deleted.

2 changes: 2 additions & 0 deletions DjangoExampleProject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
]}

MINIO_ENDPOINT = os.getenv("GH_MINIO_ENDPOINT", "play.min.io")
MINIO_EXTERNAL_ENDPOINT = os.getenv("GH_MINIO_EXTERNAL_ENDPOINT", "externalplay.min.io")
MINIO_EXTERNAL_ENDPOINT_USE_HTTPS = bool(distutils.util.strtobool(os.getenv("GH_MINIO_EXTERNAL_ENDPOINT_USE_HTTPS", "true")))
MINIO_ACCESS_KEY = os.getenv("GH_MINIO_ACCESS_KEY", "Q3AM3UQ867SPQQA43P2F")
MINIO_SECRET_KEY = os.getenv("GH_MINIO_SECRET_KEY", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
MINIO_USE_HTTPS = bool(distutils.util.strtobool(os.getenv("GH_MINIO_USE_HTTPS", "true")))
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ from datetime import timedelta
from typing import List, Tuple

MINIO_ENDPOINT = 'minio.your-company.co.uk'
MINIO_EXTERNAL_ENDPOINT = "external-minio.your-company.co.uk" # Default is same as MINIO_ENDPOINT
MINIO_EXTERNAL_ENDPOINT_USE_HTTPS = True # Default is same as MINIO_USE_HTTPS
MINIO_ACCESS_KEY = 'yourMinioAccessKey'
MINIO_SECRET_KEY = 'yourVeryS3cr3tP4ssw0rd'
MINIO_USE_HTTPS = True
Expand Down
8 changes: 7 additions & 1 deletion django_minio_backend/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.apps import AppConfig
from .utils import get_setting
from .utils import get_setting, ConfigurationError


__all__ = ['DjangoMinioBackendConfig', ]
Expand All @@ -14,3 +14,9 @@ def ready(self):
from django.core.management import call_command
print("Executing consistency checks...")
call_command('initialize_buckets', silenced=True)

# Validate configuration combinations for EXTERNAL ENDPOINT
external_address = bool(get_setting('MINIO_EXTERNAL_ENDPOINT'))
external_use_https = get_setting('MINIO_EXTERNAL_ENDPOINT_USE_HTTPS')
if (external_address and external_use_https is None) or (not external_address and external_use_https):
raise ConfigurationError('MINIO_EXTERNAL_ENDPOINT must be configured together with MINIO_EXTERNAL_ENDPOINT_USE_HTTPS')
20 changes: 18 additions & 2 deletions django_minio_backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ def __init__(self,

self.__CLIENT: Union[minio.Minio, None] = None
self.__MINIO_ENDPOINT: str = get_setting("MINIO_ENDPOINT")
self.__MINIO_EXTERNAL_ENDPOINT: str = get_setting("MINIO_EXTERNAL_ENDPOINT", self.__MINIO_ENDPOINT)
self.__MINIO_ACCESS_KEY: str = get_setting("MINIO_ACCESS_KEY")
self.__MINIO_SECRET_KEY: str = get_setting("MINIO_SECRET_KEY")
self.__MINIO_USE_HTTPS: bool = get_setting("MINIO_USE_HTTPS")
self.__MINIO_EXTERNAL_ENDPOINT_USE_HTTPS: bool = get_setting("MINIO_EXTERNAL_ENDPOINT_USE_HTTPS", self.__MINIO_USE_HTTPS)

self.__BASE_URL = ("https://" if self.__MINIO_USE_HTTPS else "http://") + self.__MINIO_ENDPOINT
self.__BASE_URL_EXTERNAL = ("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] = get_setting("MINIO_PRIVATE_BUCKETS", [])
self.PUBLIC_BUCKETS: List[str] = get_setting("MINIO_PUBLIC_BUCKETS", [])
Expand Down Expand Up @@ -183,14 +187,15 @@ def url(self, name: str):
:return: (str) URL to object
"""
if self.is_bucket_public:
return f'{self.__BASE_URL}/{self._BUCKET_NAME}/{name}'
return f'{self.base_url_external}/{self._BUCKET_NAME}/{name}'

try:
return self.client.presigned_get_object(
u: str = self.client.presigned_get_object(
bucket_name=self._BUCKET_NAME,
object_name=name.encode('utf-8'),
expires=get_setting("MINIO_URL_EXPIRY_HOURS", timedelta(days=7)) # Default is 7 days
)
return u if self.same_endpoints else u.replace(self.base_url, self.base_url_external, 1)
except urllib3.exceptions.MaxRetryError:
raise ConnectionError("Couldn't connect to Minio. Check django_minio_backend parameters in Django-Settings")

Expand Down Expand Up @@ -232,6 +237,13 @@ def _guess_content_type(file_path_name: str, content: InMemoryUploadedFile):
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:
return self._BUCKET_NAME
Expand Down Expand Up @@ -271,6 +283,10 @@ def client(self) -> minio.Minio:
def base_url(self) -> str:
return self.__BASE_URL

@property
def base_url_external(self) -> str:
return self.__BASE_URL_EXTERNAL

def new_client(self):
"""
Instantiates a new Minio client and
Expand Down

0 comments on commit 418fc13

Please sign in to comment.