Kellton LogoHome
Contact

Simplify your Django deployment with MinIO

Want to make your Django projects easier? Discover how MinIO can streamline local S3 storage, enhancing your development and deployment process. Find out how to seamlessly integrate MinIO with Django and connect local and cloud environments for smoother, more reliable applications.

Mateusz Jasiński
7 min read
3D illustration of cloud computing concept with a glowing purple cloud containing server racks connected to a computer monitor on a desk.

Using MinIO with Django for local development

The development process of modern applications often involves discrepancies between local and cloud environments, necessitating thorough testing in both settings. In this article, we'll explore a solution that simplifies replicating AWS S3 bucket functionality locally using MinIO, enhancing your Django development workflow. This approach is particularly beneficial for those looking to streamline their Django MinIO integration. For more insights on enhancing your Django projects, explore our expertise in Django development services.

What is MinIO?

MinIO is an open-source object storage service that emulates the AWS S3 API, allowing developers to create multiple buckets, upload files, and manage them through a user-friendly dashboard. By integrating MinIO into your local development environment, you can test S3 communication and features without the need for multiple environment configurations, thus streamlining application deployment. This makes MinIO for Django development an attractive option for developers seeking efficient local S3 storage with MinIO.

For a deeper understanding of MinIO, visit their official website. Below are some screenshots showcasing the MinIO dashboard's interface.

Docker configuration for MinIO

To leverage MinIO in your local setup, you'll need to configure two additional containers in your docker-compose.yml file:

minio

The main container that will simulate your S3 service.

minio-client

The additional container that will create your buckets automatically.

Here's a sample configuration:

volumes:
  minio-data:
minio:
  image: minio/minio:latest
  ports:
    - 9000:9000
    - 9001:9001
  environment:
    MINIO_ACCESS_KEY: minio
    MINIO_SECRET_KEY: minio123
  command: server /data --console-address ":9001"
  volumes:
    - minio-data:/data
minio-client:
  image: minio/mc:latest
  entrypoint: >
    /bin/sh -c "  
    /usr/bin/mc config host rm expo;  
    /usr/bin/mc config host add --quiet --api s3v4 local http://minio:9000 minio minio123;  
    /usr/bin/mc rb --force local/<your-bucket-name>/;  
    /usr/bin/mc mb --quiet local/<your-bucket-name>/;  
    /usr/bin/mc policy set public local/<your-bucket-name>;  
    "
  depends_on:
    - minio

This setup creates a single MinIO server running on port 9000 and automatically generates a bucket using the client. The configuration utilizes volumes to ensure data persistence. If you prefer to clear your bucket upon stopping the containers, simply remove the volume references from the minio container configuration.

Django configuration with MinIO

To integrate MinIO with Django, you'll need to set up custom storage classes and modify your settings file. This involves configuring Django custom storage classes to ensure cohesive interaction with MinIO.

Custom storage

The default S3Boto3Storage requires adjustments to work seamlessly with MinIO. Since we're using Docker, the MinIO container is accessible within the Docker network at minio:9000, but externally, it's available at localhost:9000. To handle this and ensure proper path handling for browsers, we'll create custom storage classes:

class StaticS3Boto3Storage(S3Boto3Storage):
    location = settings.STATICFILES_LOCATION

    def __init__(self, *args, **kwargs):
        if settings.MINIO_ACCESS_URL:
            self.secure_urls = False
            self.custom_domain = settings.MINIO_ACCESS_URL
        super(StaticS3Boto3Storage, self).__init__(*args, **kwargs)

class S3MediaStorage(S3Boto3Storage):
    def __init__(self, *args, **kwargs):
        if settings.MINIO_ACCESS_URL:
            self.secure_urls = False
            self.custom_domain = settings.MINIO_ACCESS_URL
        super(S3MediaStorage, self).__init__(*args, **kwargs)

Settings file configuration

In your Django settings.py file, configure the following to use S3 with MinIO:

STATIC_URL = "/static/"
STATICFILES_LOCATION = "static"
STATICFILES_STORAGE = "blogs.storage.StaticS3Boto3Storage"

MEDIA_URL = "/media/"
DEFAULT_FILE_STORAGE = "blogs.storage.S3MediaStorage"

AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STORAGE_BUCKET_NAME")

AWS_S3_ENDPOINT_URL = os.getenv("AWS_S3_URL")
MINIO_ACCESS_URL = os.getenv("MINIO_ACCESS_URL")

And in your .env file:

AWS_ACCESS_KEY_ID=minio
AWS_SECRET_ACCESS_KEY=minio123
AWS_STORAGE_BUCKET_NAME=<your-bucket-name>
AWS_S3_URL=http://minio:9000  
MINIO_ACCESS_URL=localhost:9000/<your-bucket-name>

How we use MinIO in Kellton Europe

At Kellton Europe, we've found numerous applications for MinIO beyond the basic setup described here. Some of our advanced uses include:

01

Creating dedicated buckets for dynamic development environments, allowing for more flexible testing scenarios.

02

Pretesting access to private buckets for highly secure applications, ensuring data protection measures are in place before deployment.

03

Integrating MinIO into our CI/CD pipelines for automated integration testing, which helps catch S3-related issues early in the development cycle.

Summary

By incorporating MinIO into your local Django development environment, you can significantly simplify the process of testing and deploying applications that rely on S3 storage. This approach not only reduces the complexity of managing different configurations but also enhances the reliability of your deployments. The MinIO vs AWS S3 comparison often highlights MinIO's advantages in local development scenarios due to its open-source nature and ease of use.

For those interested in seeing this setup in action, see an example project available on GitHub. If you need professional assistance implementing MinIO, Django, or other web development solutions, our experienced development team at Kellton Europe is here to help.

Portrait of a man in a suit and glasses, looking composed against a neutral background.

Mateusz Jasiński

Engineering Manager

A grown-up kid who replaced toys with tech tools - developer by day, greenkeeper by night. Bugs and weeds don’t stand a chance.

A man standing in the office in front of the Kellton sign, wearing a black shirt and glasses.

Sebastian Spiegel

Backend Development Director

Inspired by our insights? Let's connect!

You've read what we can do. Now let's turn our expertise into your project's success!

Get in touch with us

0 / 3000
Let us know you're human
By submitting this form you acknowledge that you have read Kellton's Privacy Policy and agree to its terms.

Get to know us

Learn about our team, values, and commitment to delivering high-quality, tailored solutions for your business.

Tell us about your needs

Share your project requirements and objectives so we can craft a customized plan.

Free consultation

Make the most of our free consultation to discover the optimal strategies and solutions tailored to your business challenges.