FastAPI MinIO Integration

fastapi

Storing and retrieving files in modern applications is a must-have feature. With FastAPI’s performance and simplicity, paired with MinIO’s lightweight, S3-compatible object storage, you have a powerful stack for building scalable, file-heavy applications. In this tutorial, we’ll walk through how to integrate FastAPI with MinIO, fully containerized using Docker Compose.

What You’ll Learn

  • What MinIO is and why it’s useful
  • How to configure a Docker Compose environment for FastAPI and MinIO
  • How to upload and retrieve files from MinIO using FastAPI
  • Using the boto3 library to interact with MinIO

What is MinIO?

MinIO is a high-performance, Kubernetes-native object storage system that’s compatible with Amazon S3 APIs. It allows developers to self-host S3-like storage, perfect for local development and private deployments.

Project Structure

fastapi-minio/
├── app/
│ ├── main.py
│ └── minio_client.py
├── .env
├── Dockerfile
├── docker-compose.yml
└── requirements.txt

Step 1: Docker Compose Setup

Step 2: FastAPI Application main.py

In main.py file put these codes:

Step 4: FastAPI Application minio_client.py

Step 5: Dockerfile for FastAPI

Step 6: requirements.txt

Step 7: Running the project

Step 8: Testing the project

Since we are using FastAPI we don’t need any API Testing Client as we have access to Swagger in this address:

http://localhost:8000/doc

Click on “upload” api and click on “Try it out”, upload a file you will see that it gets succeeded.

For download you can click on “download” api and put the name of the file in the field and just run and you will see that you get a “download” button to download the file.

By the way the source code of project is here:

https://github.com/mjmichael73/fastapi-minio-integration

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *