The django-minio-backend provides a wrapper around the MinIO Python Library.
Project description
django-minio-backend
The django-minio-backend
provides a wrapper around the
MinIO Python SDK.
Integration
-
Get and install the package:
pip install django-minio-backend
-
Add
django_minio_backend
toINSTALLED_APPS
:
INSTALLED_APPS = [
'...'
'django_minio_backend', # django-minio-backend | https://github.com/theriverman/django-minio-backend
]
If you would like to enable on-start consistency check, install via DjangoMinioBackendConfig
:
INSTALLED_APPS = [
'...'
'django_minio_backend.apps.DjangoMinioBackendConfig', # django-minio-backend | https://github.com/theriverman/django-minio-backend
]
Then add the following parameter to your settings file:
MINIO_CONSISTENCY_CHECK_ON_START = True
Note: The on-start consistency check equals to manually calling python manage.py initialize_buckets
.
It is recommended to turn off this feature during development,
because this function can noticeably slow down Django's boot time.
- Add the following parameters to your
settings.py
:
from datetime import timedelta
from typing import List, Tuple
MINIO_ENDPOINT = 'minio.yourcompany.co.uk'
MINIO_ACCESS_KEY = 'yourMinioAccessKey'
MINIO_SECRET_KEY = 'yourVeryS3cr3tP4ssw0rd'
MINIO_USE_HTTPS = True
MINIO_URL_EXPIRY_HOURS = timedelta(days=1) # Default is 7 days (longest) if not defined
MINIO_CONSISTENCY_CHECK_ON_START = True
MINIO_PRIVATE_BUCKETS = [
'django-backend-dev-private',
]
MINIO_PUBLIC_BUCKETS = [
'django-backend-dev-public',
]
MINIO_POLICY_HOOKS: List[Tuple[str, dict]] = []
- Implement your own Attachment handler and integrate django-minio-backend:
from django.db import models
from django_minio_backend import MinioBackend, iso_date_prefix
# noinspection PyUnresolvedReferences
class PrivateAttachment(models.Model):
file = models.FileField(verbose_name="Object Upload",
storage=MinioBackend(bucket_name='my-private-bucket'),
upload_to=iso_date_prefix)
-
Initialize buckets & set public policy (OPTIONAL):
Thisdjango-admin
command creates both the private and public buckets in case one of them does not exists and sets the public bucket's privacy policy from private(default) to public.
python manage.py initialize_buckets
Code reference: initialize_buckets.py.
Health Check
To check the connection link between Django and MinIO, use the provided MinioBackend.is_minio_available()
method.
It returns a MinioServerStatus
instance which can be quickly evaluated as boolean.
Example:
from django_minio_backend import MinioBackend
minio_available = MinioBackend('').is_minio_available() # An empty string is fine this time
if minio_available:
print("OK")
else:
print("NOK")
print(minio_available.details)
Policy Hooks
You can configure django-minio-backend to automatically execute a set of pre-defined policy hooks.
Policy hooks can be defined in settings.py
by adding MINIO_POLICY_HOOKS
which must be list of tuples.
Policy hooks are automatically picked up by the initialize_buckets
management command.
For an exemplary policy, see the contents of def set_bucket_to_public(self)
in models.py.
Reference Implementation
For a reference implementation, see Examples.
Compatibility
- Django 2.0 or later
- Python 3.6.0 or later
Note: This library relies heavily on PEP 484 -- Type Hints which was introduced in Python 3.5.0.
Contribution
To build a new package, execute the following command:
python setup.py sdist
Copyright
- theriverman/django-minio-backend licensed under the MIT License
- minio/minio-py is licensed under the Apache License 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-minio-backend-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50f9e1e3ac5f8935c8c36d4942e6adab29ca968eb16953ebac8d9bc90981218f |
|
MD5 | c46dc72397fbaf5ca3ea7eaa510b72d6 |
|
BLAKE2b-256 | 9736d477adec0349eab6d7cbb31240b6c7d5eb40dd508be7f832b3a8fdf319bc |