MinIO Testcontainers

0.0.1rc1 · active · verified Thu Apr 16

The `testcontainers-minio` library is a component of `testcontainers-python`, an active library designed for functional and integration testing with throwaway Docker containers. It simplifies the setup and teardown of MinIO object storage instances for tests. While `testcontainers-python` is currently at version 4.x.x, the `testcontainers-minio` module is at an early release candidate, `0.0.1rc1`. The parent project `testcontainers-python` maintains a frequent release cadence, continuously adding new features and bug fixes across its various modules.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `MinioContainer` to spin up a MinIO server, retrieve its connection details, and then use the `minio` client library to create a bucket, upload an object, and retrieve it. The container is automatically managed (started and stopped) using a `with` statement.

import io
from testcontainers.minio import MinioContainer
from minio import Minio

# Instantiate MinioContainer (it automatically starts on entering 'with')
with MinioContainer() as minio_container:
    # Get MinIO client configuration from the container
    minio_config = minio_container.get_config()
    endpoint = minio_config['endpoint']
    access_key = minio_config['access_key']
    secret_key = minio_config['secret_key']

    # Create a MinIO client instance to interact with the container
    client = Minio(
        endpoint,
        access_key=access_key,
        secret_key=secret_key,
        secure=False  # Testcontainers MinIO usually runs on http by default
    )

    bucket_name = "test-bucket"
    object_name = "hello.txt"
    file_content = b"Hello from Testcontainers MinIO!"

    # Create a bucket if it doesn't exist
    if not client.bucket_exists(bucket_name):
        client.make_bucket(bucket_name)
        print(f"Bucket '{bucket_name}' created.")

    # Upload an object
    client.put_object(
        bucket_name,
        object_name,
        io.BytesIO(file_content),
        length=len(file_content),
        content_type="text/plain"
    )
    print(f"Object '{object_name}' uploaded to '{bucket_name}'.")

    # Retrieve the object
    response = client.get_object(bucket_name, object_name)
    retrieved_data = response.read()
    response.close()
    response.release_conn()

    print(f"Retrieved data: {retrieved_data.decode('utf-8')}")
    assert retrieved_data == file_content
    print("MinIO Testcontainers setup successful!")

view raw JSON →