MinIO: Connecting Python to MinIO

This documentation is part of the Getting started guide. View the full guide here: How to begin using MinIO for S3-compatible object storage.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use MinIO Object Storage solution that provides a host of benefits, including:

  • Unlimited transfers.
  • Simple, predictive, and transparent pricing.
  • Customizable domain name secured with HTTPS (for example, https://object-storage.your-company.com).
  • Effortless updates with just a click.
  • Optimal performance and robust security powered by a private and dedicated VM.
  • Available in 🇪🇺 Europe and 🇺🇸 USA.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's MinIO Object Storage hosting solution!

There are two primary ways to connect Python applications to MinIO.

First, install the AWS SDK package (boto3):

pip install boto3
pip freeze > requirements.txt

The following example demonstrates how to connect to MinIO using boto3:

#!/usr/bin/env python
import os
import boto3
from botocore.client import Config

s3 = boto3.resource('s3',
                    endpoint_url='https://<XXXXXX>.stackhero-network.com',
                    aws_access_key_id='YOUR_ACCESS_KEY',
                    aws_secret_access_key='YOUR_SECRET_KEY',
                    config=Config(signature_version='s3v4'),
                    region_name='us-east-1')

# Upload a local file '/home/john/piano.mp3' to bucket 'songs' with the object name 'piano.mp3'
s3.Bucket('songs').upload_file('/home/john/piano.mp3', 'piano.mp3')

# Download the object 'piano.mp3' from bucket 'songs' and save it locally as '/tmp/classical.mp3'
s3.Bucket('songs').download_file('piano.mp3', '/tmp/classical.mp3')

print("'piano.mp3' downloaded as 'classical.mp3'.")

More details are available in the MinIO Python documentation.

Alternatively, you can use the MinIO SDK for Python. First, install the MinIO package:

pip install minio
pip freeze > requirements.txt

Below is an example showing how to connect to MinIO using the MinIO SDK:

#!/usr/bin/env python
from minio import Minio
from minio.error import S3Error

def main():
  # Create a client for your MinIO server using your access and secret keys
  client = Minio(
    endpoint='<XXXXXX>.stackhero-network.com:443',
    secure=True,
    access_key='YOUR_ACCESS_KEY',
    secret_key='YOUR_SECRET_KEY'
  )

  # Check if the bucket 'asiatrip' exists, and create it if it does not
  found = client.bucket_exists("asiatrip")
  if not found:
    client.make_bucket("asiatrip")
  else:
    print("Bucket 'asiatrip' already exists")

  # Upload '/home/user/Photos/asiaphotos.zip' as 'asiaphotos-2015.zip' to the bucket 'asiatrip'
  client.fput_object(
    "asiatrip", "asiaphotos-2015.zip", "/home/user/Photos/asiaphotos.zip"
  )
  print(
    "'/home/user/Photos/asiaphotos.zip' has been successfully uploaded as "
    "object 'asiaphotos-2015.zip' to bucket 'asiatrip'."
  )


if __name__ == "__main__":
  try:
    main()
  except S3Error as exc:
    print("An error occurred.", exc)

For further guidance, see the MinIO Python SDK documentation.