Skip to main content

The django-minio-backend provides a wrapper around the MinIO Python Library.

Project description

Board Status Build Status

django-minio-backend

The django-minio-backend provides a wrapper around the MinIO Python SDK.

Integration

  1. Get and install the package:
pip install django-minio-backend
  1. Add django_minio_backend to INSTALLED_APPS:
INSTALLED_APPS = [
    '...'
    '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',  # 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 by setting MINIO_CONSISTENCY_CHECK_ON_START to False, because this operation can noticeably slow down Django's boot time when many buckets are configured.

  1. Add the following parameters to your settings.py:
from datetime import timedelta
from typing import List, Tuple

MINIO_ENDPOINT = 'minio.your-company.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]] = []
  1. Implement your own Attachment handler and integrate django-minio-backend:
from django.db import models
from django_minio_backend import MinioBackend, iso_date_prefix

class PrivateAttachment(models.Model):   
    file = models.FileField(verbose_name="Object Upload",
                            storage=MinioBackend(bucket_name='django-backend-dev-private'),
                            upload_to=iso_date_prefix)
  1. Initialize the buckets & set their public policy (OPTIONAL):
    This django-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 a 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.

Consistency Check On Start

When enabled, the initialize_buckets management command gets called automatically when Django starts.
This command connects to the configured minIO server and checks if all buckets defined in settings.py.
In case a bucket is missing or its configuration differs, it gets created and corrected.

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

Local Test Environment

To setup your local test environment, execute the following steps:

  1. Clone the repository
git clone git@github.com:theriverman/django-minio-backend.git
  1. Move to example_app:
cd django-minio-backend/example_app
  1. Create the virtual environment:
pipenv install --dev

Note: On Windows, you might be required to move to the parent directory (cd ..) and execute the above command there. 3. Activate the virtual environment (if not active yet):

pipenv shell
  1. Apply migrations:
python manage.py migrate
  1. Start Django
python manage.py runserver

Copyright

  • theriverman/django-minio-backend licensed under the MIT License
  • minio/minio-py is licensed under the Apache License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-minio-backend-2.2.5.tar.gz (11.2 kB view details)

Uploaded Source

File details

Details for the file django-minio-backend-2.2.5.tar.gz.

File metadata

  • Download URL: django-minio-backend-2.2.5.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.10

File hashes

Hashes for django-minio-backend-2.2.5.tar.gz
Algorithm Hash digest
SHA256 19bc4cccb075670c61394023dbe5d2850a83b086db165655fd4e7dcfc53d124c
MD5 1e2953f2f6c843d3341439d3def57c0b
BLAKE2b-256 bcc47d25359d2bbc1bbec9014443adedb1d41b265c982054cd74a18a7e9bea97

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page