MinIO Python Client SDK

repository·master·Indexed 22 days ago

https://github.com/minio/minio-py

A Python SDK providing high-level APIs to access MinIO Object Storage or any Amazon S3 compatible service. It supports bucket and object management, CORS, notifications, encryption, versioning, replication, and lifecycle configurations. For high-performance workloads, it offers optional RDMA and GPUDirect Storage support via minio-cpp. Requires Python 3.10 or higher.

Tokens
27.8K
Snippets
79
Records
88
Agent score
76%

What's inside minio-py

  1. Enable RDMA / GPUDirect Storage (Optional)

    master

    For high-performance workloads, put_object and get_object can dispatch to MinIO's RDMA + GPUDirect Storage path via minio-cpp. This is an opt-in feature.

    Requirements

    • libminiocpp.so must be on the host's library search path (or specified via the MINIOCPP_LIB environment variable).
    • libminiocpp.so must be built with -DMINIO_CPP_ENABLE_RDMA=ON.

    Usage

    1. Set enable_rdma=True when initializing the Minio client.
    2. Use a buffer-protocol object (like bytearray) for the data= argument in put_object or the into= argument in get_object to trigger RDMA.
    3. For GPU buffers (e.g., CuPy or PyTorch), pass the raw pointer as an int to data= or into=.

    Warning: The SDK remains pure-Python unless these specific conditions are met.

    from minio import Minio
    
    client = Minio(
        endpoint="server:9000",
        access_key="...",
        secret_key="...",
        secure=False,
        enable_rdma=True,             # opt-in
    )
    
    buf = bytearray(1 << 20)
    client.put_object(
        bucket_name="b", object_name="o",
        data=buf, length=len(buf),    # buffer-protocol object selects RDMA
    )
    
    dst = bytearray(1 << 20)
    n = client.get_object(
        bucket_name="b", object_name="o",
        into=dst, length=len(dst),    # into= selects RDMA, returns bytes
    )
  2. Configure Server-Side Encryption (SSE) for uploads

    master

    When using put_object or fput_object, you can specify how the data is encrypted on the server using the sse parameter. Supported types include:

    • S3-managed keys: Use SseS3().
    • KMS-managed keys: Use SseKMS(key_id, context) where key_id is the KMS key ID and context is a dictionary of encryption context.
    • Customer-provided keys: Use SseCustomerKey(key) where key is a 32-byte secret key.
    # Upload data with customer key type of server-side encryption.
    result = client.put_object(
        bucket_name="my-bucket",
        object_name="my-object",
        data=io.BytesIO(b"hello"),
        length=5,
        sse=SseCustomerKey(b"32byteslongsecretkeymustprovided"),
    )
    
    # Upload data with KMS type of server-side encryption.
    result = client.put_object(
        bucket_name="my-bucket",
        object_name="my-object",
        data=io.BytesIO(b"hello"),
        length=5,
        sse=SseKMS("KMS-KEY-ID", {"Key1": "Value1", "Key2": "Value2"}),
    )
    
    # Upload data with S3 type of server-side encryption.
    result = client.put_object(
        bucket_name="my-bucket",
        object_name="my-object",
        data=io.BytesIO(b"hello"),
        length=5,
        sse=SseS3(),
    )
  3. Create a Minio client with various authentication methods

    master

    Depending on your security requirements, you can initialize the Minio client in several ways:

    • Anonymous access: Provide only the endpoint.
    • Access and Secret keys: Provide endpoint, access_key, and secret_key.
    • Specific Region: Provide endpoint, access_key, secret_key, and a region string.
    • Custom HTTP Client (Proxy): Provide a custom urllib3.PoolManager via the http_client parameter to route requests through a proxy server.
  4. Create buckets with specific configurations

    master

    When using make_bucket, you can perform the following tasks:

    • Standard bucket creation: Create a bucket with just the bucket_name.
    • Regional bucket creation: Specify a location to create the bucket in a specific region.
    • Object-lock enabled bucket: Set object_lock=True and specify a location to create a bucket with the object-lock feature enabled in a specific region.
  5. Install the MinIO Python SDK

    master

    The MinIO Python SDK requires Python version 3.10 or higher. You can install it using pip or by cloning the source from GitHub.

    Using pip

    Install the package directly from PyPI:

    pip3 install minio

    Using Source From GitHub

    Clone the repository and install using setup.py:

    git clone https://github.com/minio/minio-py
    cd minio-py
    python setup.py install
    pip3 install minio
  6. Initialize a MinIO Client

    master

    To connect to an Amazon S3 compatible object storage service, initialize the Minio client with the following parameters:

    ParameterDescription
    endpointThe URL of the object storage service.
    access_keyThe unique User ID for your account.
    secret_keyThe password for your account.
    secureBoolean indicating whether to use HTTPS (set to True for HTTPS).
    from minio import Minio
    
    minioClient = Minio('play.min.io',
                      access_key='YOUR_ACCESS_KEY',
                      secret_key='YOUR_SECRET_KEY',
                      secure=True)
    from minio import Minio
    
    minioClient = Minio('play.min.io',
                      access_key='Q3AM3UQ867SPQQA43P2F',
                      secret_key='zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
                      secure=True)
  7. Upload data with tags, retention, and legal hold using `fput_object`

    master

    You can upload a file from a local path while simultaneously applying metadata tags, object retention policies (e.g., GOVERNANCE mode), and legal hold status. This is useful for compliance and data lifecycle management.

    # Upload data with tags, retention and legal-hold.
    date = datetime.utcnow().replace(
        hour=0, minute=0, second=0, microsecond=0,
    ) + timedelta(days=30)
    tags = Tags(for_object=True)
    tags["User"] = "jsmith"
    result = client.fput_object(
        bucket_name="my-bucket",
        object_name="my-object",
        file_path="my-filename",
        tags=tags,
        retention=Retention(GOVERNANCE, date),
        legal_hold=True,
    )
    print(
        f"created {result.object_name} object; etag: {result.etag}, "
        f"version-id: {result.version_id}",
    )
  8. Example: Create a bucket and upload a file

    master

    This example demonstrates how to connect to a MinIO service, create a bucket (handling cases where it already exists), and upload a local file to that bucket using fput_object.

    # 引入MinIO包。
    from minio import Minio
    from minio.error import (ResponseError, BucketAlreadyOwnedByYou,
                             BucketAlreadyExists)
    
    # 使用endpoint、access key和secret key来初始化minioClient对象。
    minioClient = Minio('play.min.io',
                        access_key='Q3AM3UQ867SPQQA43P2F',
                        secret_key='zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
                        secure=True)
    
    # 调用make_bucket来创建一个存储桶。
    try:
           minioClient.make_bucket("maylogs", location="us-east-1")
    except BucketAlreadyOwnedByYou as err:
           pass
    except BucketAlreadyExists as err:
           pass
    except ResponseError as err:
           raise
    else:
            try:
                   minioClient.fput_object('maylogs', 'pumaserver_debug.log', '/tmp/pumaserver_debug.log')
            except ResponseError as err:
                   print(err)
  9. Upload data with a progress bar using `fput_object`

    master

    When uploading files using fput_object, you can pass a Progress instance to the progress parameter to track the upload status.

    # Upload data with progress bar.
    result = client.fput_object(
        bucket_name="my-bucket",
        object_name="my-object",
        file_path="my-filename",
        progress=Progress(),
    )
    print(
        f"created {result.object_name} object; etag: {result.etag}, "
        f"version-id: {result.version_id}",
    )
  10. Example: Upload a file to a bucket

    master

    This example demonstrates how to connect to a MinIO server, check if a bucket exists (creating it if necessary), and upload a local file to a specific bucket using fput_object.

    Steps:

    1. Initialize the Minio client.
    2. Use client.bucket_exists(bucket_name=...) to check for the bucket.
    3. Use client.make_bucket(bucket_name=...) to create the bucket if it is missing.
    4. Use client.fput_object(...) to upload the file from a local path.

    Note: This example uses the public MinIO play server. Data uploaded here is public and world-readable.

    # file_uploader.py MinIO Python SDK example
    from minio import Minio
    from minio.error import S3Error
    
    def main():
        # Create a client with the MinIO server playground, its access key
        # and secret key.
        client = Minio(
            endpoint="play.min.io",
            access_key="Q3AM3UQ867SPQQA43P2F",
            secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
        )
    
        # The file to upload, change this path if needed
        source_file = "/tmp/test-file.txt"
    
        # The destination bucket and filename on the MinIO server
        bucket_name = "python-test-bucket"
        destination_file = "my-test-file.txt"
        
        # Make the bucket if it doesn't exist.
        found = client.bucket_exists(bucket_name=bucket_name)
        if not found:
            client.make_bucket(bucket_name=bucket_name)
            print("Created bucket", bucket_name)
        else:
            print("Bucket", bucket_name, "already exists")
    
        # Upload the file, renaming it in the process
        client.fput_object(
            bucket_name=bucket_name,
            object_name=destination_file,
            file_path=source_file,
        )
        print(
            source_file, "successfully uploaded as object",
            destination_file, "to bucket", bucket_name,
        )
    
    if __name__ == "__main__":
        try:
            main()
        except S3Error as exc:
            print("error occurred.", exc)
  11. Upload files via POST using `presigned_post_policy` data with `curl`

    master

    Once you have obtained the signed_form_data from minioClient.presigned_post_policy(post_policy), you can construct a curl command to upload a file. The form data consists of a base URL (the first element) and a set of key-value pairs (the subsequent elements) that must be sent as part of a multipart/form-data POST request.

    # Assuming signed_form_data was obtained from presigned_post_policy
    curl_str = 'curl -X POST {0}'.format(signed_form_data[0])
    curl_cmd = [curl_str]
    for field in signed_form_data[1]:
        curl_cmd.append('-F {0}={1}'.format(field, signed_form_data[1][field]))
    
    # print curl command to upload files.
    curl_cmd.append('-F file=@<FILE>')
    print(' '.join(curl_cmd))
  12. Upload data using put_object

    master

    Use put_object to upload data from a file-like object (an object with a callable read() method returning bytes) to a bucket. This is useful for streaming data or uploading in-memory buffers.

    Key Parameters:

    • bucket_name: Name of the destination bucket.
    • object_name: Name of the object to create.
    • data: An io.BinaryIO object containing the data.
    • length: Data size in bytes. Use -1 for unknown size (requires setting part_size).
    • content_type: The MIME type of the object (defaults to application/octet-stream).
    • metadata: A dictionary for user metadata.
    • sse: Server-side encryption configuration (e.g., SseS3, SseKMS, or SseCustomerKey).
    • tags: An instance of minio.models.Tags.
    • retention: Retention configuration.
    • legal_hold: Boolean flag to set legal hold.
    • progress: A progress object to track upload status.
    • part_size: Multipart part size (required if length is -1).

    Returns: minio.models.ObjectWriteResponse containing object_name, etag, and version_id.

    import io
    
    # Upload data.
    result = client.put_object(
        bucket_name="my-bucket",
        object_name="my-object",
        data=io.BytesIO(b"hello"),
        length=5,
    )
    print(
        f"created {result.object_name} object; etag: {result.etag}, "
        f"version-id: {result.version_id}",
    )
    
    # Upload unknown sized data using a stream.
    with urlopen(
        "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.4.81.tar.xz",
    ) as data:
        result = client.put_object(
            bucket_name="my-bucket",
            object_name="my-object",
            data=data,
            length=-1,
            part_size=10*1024*1024,
        )