Afin d’uploader une chaîne Python comme
upload_string_boto3_example.py
my_string = "This shall be the content for a file I want to create on an S3-compatible storage"vers un stockage compatible S3 comme Wasabi ou Amazon S3, vous devez l’encoder en utilisant .encode("utf-8") et puis l’envelopper dans un objet io.BytesIO :
upload_fileobj_snippet.py
my_bucket.upload_fileobj(io.BytesIO(my_string.encode("utf-8")), "myfile.txt")Exemple complet :
upload_string_full_example.py
import boto3
import io
# Crée la connexion à Wasabi / S3
s3 = boto3.resource('s3',
endpoint_url = 'https://s3.eu-central-1.wasabisys.com',
aws_access_key_id = 'MY_ACCESS_KEY',
aws_secret_access_key = 'MY_SECRET_KEY'
)
# Obtient l'objet bucket
my_bucket = s3.Bucket('boto-test')
# Uploade la chaîne vers un fichier
my_string = "This shall be the content for a file I want to create on an S3-compatible storage"
my_bucket.upload_fileobj(io.BytesIO(my_string.encode("utf-8")), "myfile.txt")N’oubliez pas de remplir MY_ACCESS_KEY et MY_SECRET_KEY. Selon la région et le service compatible S3 que vous utilisez, vous pourriez avoir besoin d’utiliser une autre URL d’endpoint au lieu de https://s3.eu-central-1.wasabisys.com.
Si cet article vous a aidé, pensez à m'offrir un café ou à faire un don via PayPal pour soutenir la recherche et la publication de nouveaux articles sur TechOverflow