String als Wasabi/S3-Objekt mit boto3 in Python hochladen
English
Deutsch
Um einen Python-String wie
upload_string_boto3_example.py
my_string = "This shall be the content for a file I want to create on an S3-compatible storage"auf einen S3-kompatiblen Speicher wie Wasabi oder Amazon S3 hochzuladen, muss er mit .encode("utf-8") kodiert und dann in ein io.BytesIO-Objekt gewickelt werden:
upload_fileobj_snippet.py
my_bucket.upload_fileobj(io.BytesIO(my_string.encode("utf-8")), "myfile.txt")Vollständiges Beispiel:
upload_string_full_example.py
import boto3
import io
# Verbindung zu Wasabi / S3 herstellen
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'
)
# Bucket-Objekt abrufen
my_bucket = s3.Bucket('boto-test')
# String als Datei hochladen
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")Nicht vergessen, MY_ACCESS_KEY und MY_SECRET_KEY auszufüllen. Je nach Region und S3-kompatiblem Dienst muss möglicherweise eine andere Endpoint-URL anstelle von https://s3.eu-central-1.wasabisys.com verwendet werden.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow