minio_conn.py


Overview

minio_conn.py provides a singleton class RAGFlowMinio that encapsulates connection management and operations with a MinIO object storage server. This file is designed to abstract and simplify common MinIO interactions such as uploading, downloading, deleting objects, checking object existence, generating presigned URLs, and bucket management.

The class manages connection lifecycle internally, including automatic reconnection attempts upon failures, making it robust for integration into larger systems needing reliable object storage operations.


Detailed Component Documentation

Class: RAGFlowMinio

A singleton class that manages a persistent connection to a MinIO server and exposes methods for common S3-compatible storage operations.

Properties

Methods


__init__(self)

Initializes the RAGFlowMinio instance, setting up the connection to MinIO server.

Usage example:

minio_client = RAGFlowMinio()

__open__(self)

Establishes a connection to the MinIO server using credentials and host info from settings.MINIO.

Internal method - not intended to be called externally.


__close__(self)

Closes the current connection by deleting the conn attribute and setting it to None.

Internal method.


health(self) -> object

Checks the MinIO server health by:

Usage example:

result = minio_client.health()
print(result)  # Information about the test upload

put(self, bucket: str, fnm: str, binary: bytes) -> object or None

Uploads a binary object to the specified bucket and filename.

Parameters:

Usage example:

data = b"Hello, MinIO!"
minio_client.put("mybucket", "hello.txt", data)

rm(self, bucket: str, fnm: str) -> None

Removes (deletes) the specified object from a bucket.

Parameters:

Usage example:

minio_client.rm("mybucket", "hello.txt")

get(self, bucket: str, filename: str) -> bytes or None

Retrieves the contents of the specified object.

Parameters:

Usage example:

content = minio_client.get("mybucket", "hello.txt")
if content:
    print(content.decode())

obj_exist(self, bucket: str, filename: str) -> bool

Checks if an object exists in a bucket.

Parameters:

Usage example:

exists = minio_client.obj_exist("mybucket", "hello.txt")
print("Exists:", exists)

get_presigned_url(self, bucket: str, fnm: str, expires: int) -> str or None

Generates a presigned URL for downloading an object.

Parameters:

Usage example:

url = minio_client.get_presigned_url("mybucket", "hello.txt", expires=3600)
print(url)

remove_bucket(self, bucket: str) -> None

Deletes all objects inside the bucket and then removes the bucket itself.

Parameters:

Usage example:

minio_client.remove_bucket("mybucket")

Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class RAGFlowMinio {
        -conn: Minio or None
        +__init__()
        +__open__()
        +__close__()
        +health() object
        +put(bucket: str, fnm: str, binary: bytes) object or None
        +rm(bucket: str, fnm: str) None
        +get(bucket: str, filename: str) bytes or None
        +obj_exist(bucket: str, filename: str) bool
        +get_presigned_url(bucket: str, fnm: str, expires: int) str or None
        +remove_bucket(bucket: str) None
    }

Summary

minio_conn.py provides a robust, singleton-based wrapper around MinIO client operations, abstracting connection management, retries, and error handling. It offers all essential object storage operations with fault tolerance and simple interfaces, integrating tightly with project settings and utilities.

This design simplifies storage access for the rest of the application while ensuring reliability and consistent connection state management.