diff --git a/django_minio_backend/models.py b/django_minio_backend/models.py index 02df49a..f19630c 100644 --- a/django_minio_backend/models.py +++ b/django_minio_backend/models.py @@ -1,23 +1,25 @@ -# Standard python packages import io import json -from time import mktime +import mimetypes +import ssl from datetime import datetime, timedelta from pathlib import Path +from time import mktime from typing import Union, List + +# noinspection PyPackageRequirements minIO_requirement +import certifi +import minio.definitions +import minio.error +# noinspection PyPackageRequirements minIO_requirement import urllib3 -import mimetypes -# Django packages from django.core.files import File from django.core.files.storage import Storage from django.core.files.uploadedfile import InMemoryUploadedFile from django.utils.deconstruct import deconstructible from django.utils.timezone import utc -# Third-party (MinIO) packages -import minio.error -import minio.definitions from minio import Minio -# Local Packages + from .utils import MinioServerStatus, PrivatePublicMixedError, ConfigurationError, get_setting __all__ = ['MinioBackend', 'get_iso_date', 'iso_date_prefix', ] @@ -42,6 +44,7 @@ class MinioBackend(Storage): Through self._META_KWARGS, the "metadata", "sse" and "progress" fields can be set for the underlying put_object() MinIO SDK method """ + def __init__(self, bucket_name: str, *args, @@ -232,18 +235,20 @@ def is_minio_available(self) -> MinioServerStatus: mss.add_message('MINIO_ENDPOINT is not configured in Django settings') return mss - c = urllib3.HTTPSConnectionPool(self.__MINIO_ENDPOINT, cert_reqs='CERT_NONE', assert_hostname=False) - try: - r = c.request('GET', '/minio/index.html') - return MinioServerStatus(r) - except urllib3.exceptions.MaxRetryError: - mss = MinioServerStatus(None) - mss.add_message(f'Could not open connection to {self.__MINIO_ENDPOINT}/minio/index.html ...') - return mss - except Exception as e: - mss = MinioServerStatus(None) - mss.add_message(repr(e)) - return mss + with urllib3.PoolManager(cert_reqs=ssl.CERT_REQUIRED, ca_certs=certifi.where()) as http: + try: + scheme = "https" if self.__MINIO_USE_HTTPS else "http" + r = http.request('GET', f'{scheme}://{self.__MINIO_ENDPOINT}/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.__MINIO_ENDPOINT}/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: diff --git a/django_minio_backend/utils.py b/django_minio_backend/utils.py index e0926a5..6ddfcfc 100644 --- a/django_minio_backend/utils.py +++ b/django_minio_backend/utils.py @@ -1,3 +1,4 @@ +# noinspection PyPackageRequirements minIO_requirement import urllib3 from typing import Union, List from django.conf import settings @@ -26,16 +27,19 @@ def __init__(self, request: Union[urllib3.response.HTTPResponse, None]): self.data = None self.eval() + self.__OK = 'MinIO is available' + self.___NOK = 'MinIO is NOT available' + def eval(self): if not self._request: - self._details.append('There was no request provided for MinioServerStatus upon initialisation.') + self.add_message('There was no HTTP request provided for MinioServerStatus upon initialisation.') return False self.status = self._request.status self.data = self._request.data.decode() if self._request.data else 'No data available' if self.status == 403: # Request was a legal, but the server refuses to respond to it -> it's running fine self._bool = True else: - self._details.append('MinIO is not available.') + self._details.append(self.__OK) self._details.append('Reason: ' + self.data) def __bool__(self): @@ -54,8 +58,8 @@ def details(self): def __repr__(self): if self.is_available: - return 'Minio Server Available' - return 'Minio Server Not Available' + return self.__OK + return self.___NOK class PrivatePublicMixedError(Exception): diff --git a/requirements.txt b/requirements.txt index b9aa501..84ab9b5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ Django~=3.1.3 minio~=6.0.0 Pillow -urllib3~=1.26.2 setuptools~=50.3.2 diff --git a/setup.py b/setup.py index e9debf4..91eb439 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ # allow setup.py to be run from any path os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) -CURRENT_GIT_TAG = 'v2.3.1' +CURRENT_GIT_TAG = 'v2.4.0' year = datetime.datetime.now().year print(f'setup.py :: Using git tag {CURRENT_GIT_TAG}')