MinIO: Connecter Python à MinIO

Cette documentation fait partie du guide Premiers pas. Consultez le guide complet ici : Comment débuter avec MinIO pour l'object storage compatible S3.

👋 Bienvenue sur la documentation de Stackhero !

Stackhero propose une solution MinIO Object Storage prête à l'emploi qui offre de nombreux avantages, notamment :

  • Des transferts illimités.
  • Une tarification simple, prévisible et transparente.
  • Un nom de domaine personnalisable sécurisé par HTTPS (par exemple, https://object-storage.votre-entreprise.com).
  • Des mises à jour faciles en un clic.
  • Une performance optimale et une sécurité renforcée grâce à une VM privée et dédiée.
  • Disponible en 🇪🇺 Europe et 🇺🇸 USA.

Gagnez du temps et simplifiez-vous la vie : il suffit de 5 minutes pour essayer la solution d'hébergement MinIO Object Storage de Stackhero !

Il existe deux principales méthodes pour connecter des applications Python à MinIO.

Commencez par installer le package AWS SDK (boto3) :

pip install boto3
pip freeze > requirements.txt

L'exemple suivant montre comment se connecter à MinIO avec 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')

# Téléverse un fichier local '/home/john/piano.mp3' dans le bucket 'songs' sous le nom 'piano.mp3'
s3.Bucket('songs').upload_file('/home/john/piano.mp3', 'piano.mp3')

# Télécharge l'objet 'piano.mp3' du bucket 'songs' et l'enregistre localement sous '/tmp/classical.mp3'
s3.Bucket('songs').download_file('piano.mp3', '/tmp/classical.mp3')

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

Plus d'informations sont disponibles dans la documentation Python de MinIO.

Vous pouvez aussi utiliser le SDK MinIO pour Python. Installez d'abord le package MinIO :

pip install minio
pip freeze > requirements.txt

Voici un exemple de connexion à MinIO avec le SDK MinIO :

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

def main():
  # Créez un client pour votre serveur MinIO avec vos clés d'accès et secrète
  client = Minio(
    endpoint='<XXXXXX>.stackhero-network.com:443',
    secure=True,
    access_key='YOUR_ACCESS_KEY',
    secret_key='YOUR_SECRET_KEY'
  )

  # Vérifie si le bucket 'asiatrip' existe, et le crée si besoin
  found = client.bucket_exists("asiatrip")
  if not found:
    client.make_bucket("asiatrip")
  else:
    print("Bucket 'asiatrip' already exists")

  # Téléverse '/home/user/Photos/asiaphotos.zip' sous le nom 'asiaphotos-2015.zip' dans le 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)

Pour aller plus loin, consultez la documentation du SDK Python MinIO.