Upload files in MinIO using Python
MinIO is an open source distributed object storage server written in Go, designed for Private Cloud infrastructure providing S3 storage functionality.
MinIO is the best server which is suited for storing unstructured data such as photos, videos, log files, backups, and container. Size of an object can be range from a KBs to a maximum of 5TB.
MinIO server is light enough with application stacks like Redis, MySql, and Gitlab.
Installation
To install MinIO server, run these commands.
wget https://dl.min.io/server/minio/release/linux-amd64/miniochmod +x miniosudo mv ./minio /usr/games/minio
Check if it is installed, by this command.
minio --version
Create a folder and navigate into it.
mkdir miniocd minio
To run the server:
Replace admin and password if required.
MINIO_ROOT_USER=admin MINIO_ROOT_PASSWORD=password minio server ./data{1...5} --console-address :9001
The sever starts running at localhost:9001
Create a bucket and change its access policy to public.
Upload objects using Python
Install the required packages.
pip install miniopip install python-dotenv
In a basic python flask program, include this function.
from minio import Minio
from dotenv import load_dotenv
import osload_dotenv()LOCAL_FILE_PATH = os.environ.get('LOCAL_FILE_PATH')
ACCESS_KEY = os.environ.get('ACCESS_KEY')
SECRET_KEY = os.environ.get('SECRET_KEY')MINIO_API_HOST = "http://localhost:9000"MINIO_CLIENT = Minio("localhost:9000", access_key=ACCESS_KEY, secret_key=SECRET_KEY, secure=False)def main():found = MINIO_CLIENT.bucket_exists("<bucketname>")
if not found:
MINIO_CLIENT.make_bucket("<bucketname>")
else:
print("Bucket already exists") MINIO_CLIENT.fput_object("<bucketname>", "<pic.jpg>", LOCAL_FILE_PATH,) print("It is successfully uploaded to bucket")
Create an .env file and include these variables:
LOCAL_FILE_PATH="<stringtype>"ACCESS_KEY="<admin>"SECRET_KEY="<password>"
Run the python program and check the MinIO console.
Thus the required file is uploaded in MinIO bucket.
Keep Exploring !