Interact with MinIO using Python: A Step-by-Step Guide

What is MinIO?

MinIO is an open-source, high-performance object storage solution designed for unstructured data. It is lightweight, scalable, and fully compatible with Amazon S3 APIs, making it an excellent choice for cloud-native applications.

Prerequisites

To get started, you’ll need Python (version 3.x), a running MinIO server, and the MinIO Python SDK. Install the SDK using the following command:

pip install minio  

Ensure your MinIO server is set up and accessible, either locally or on a network.

Connecting to a MinIO Server

Begin by creating a MinIO client in Python. The Minio class lets you connect to the server using the access key and secret key:

from minio import Minio  

# Initialize MinIO client  
client = Minio(  
    "localhost:9000",  # MinIO server address  
    access_key="YOUR-ACCESS-KEY",  
    secret_key="YOUR-SECRET-KEY",  
    secure=False       # Use True for HTTPS  
)  
print("Connected to MinIO!")  

Working with Buckets

To manage storage, you’ll often need to create, list, or delete buckets. Here’s how:

Creating a Bucket

To create a new bucket, use the following code:

bucket_name = "my-bucket"  
if not client.bucket_exists(bucket_name):  
    client.make_bucket(bucket_name)  
    print(f"Bucket '{bucket_name}' created.")  
else:  
    print(f"Bucket '{bucket_name}' already exists.")  
Listing Buckets

To list all buckets in your MinIO server, use this code snippet:

buckets = client.list_buckets()  
for bucket in buckets:  
    print(bucket.name, bucket.creation_date)  
Deleting a Bucket

To delete a bucket, ensure it is empty, then run the following code:

client.remove_bucket(bucket_name)  
print(f"Bucket '{bucket_name}' deleted.")  

Uploading a File

To upload a file to a bucket, use the fput_object method:

client.fput_object(bucket_name, "my-object", "path/to/local/file.txt")  
print("File uploaded successfully.")

Downloading a File

To download a file from a bucket, use the fget_object method:

client.fget_object(bucket_name, "my-object", "path/to/save/file.txt")  
print("File downloaded successfully.")

Uploading an In-Memory File

You can also upload a file from memory using the following code:

import io  

data = io.BytesIO(b"Hello, MinIO!")  
client.put_object(bucket_name, "hello.txt", data, length=len(data.getvalue()))  
print("In-memory file uploaded.")

Listing Objects in a Bucket

To list all objects in a bucket, use the list_objects method:

objects = client.list_objects(bucket_name)  
for obj in objects:  
    print(obj.object_name, obj.last_modified, obj.size)

Deleting an Object

To delete an object from a bucket, use the remove_object method:

client.remove_object(bucket_name, "my-object")  
print("Object deleted.")  

Generating a Presigned URL

MinIO allows you to generate temporary presigned URLs for accessing objects:

url = client.presigned_get_object(bucket_name, "my-object", expires=3600)  
print(f"Presigned URL: {url}")

Security and Performance Tips

For secure and efficient usage, always use HTTPS connections and manage your access credentials wisely. Routinely review and clean up unused resources to maintain an optimized setup.

Conclusion

MinIO, combined with Python, offers a robust and flexible solution for object storage needs. From basic bucket management to advanced features like presigned URLs, it simplifies storage operations for developers. Dive deeper into MinIO’s capabilities and integrate it into your next project!

Share your passion