How to download Wasabi/S3 object to string/bytes using boto3 in Python
You can use io.BytesIO
to store the content of an S3 object in memory and then convert it to bytes
which you can then decode to a str
. The following example downloads myfile.txt
into memory:
# Download to file
buf = io.BytesIO()
my_bucket.download_fileobj("myfile.txt", buf)
# Get file content as bytes
filecontent_bytes = buf.getvalue()
# ... or convert to string
filecontent_str = buf.getvalue().decode("utf-8")
Full example
import boto3
import io
# Create connection to 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'
)
# Get bucket object
my_bucket = s3.Bucket('boto-test')
# Download to file
buf = io.BytesIO()
my_bucket.download_fileobj("myfile.txt", buf)
# Get file content as bytes
filecontent_bytes = buf.getvalue()
# ... or convert to string
filecontent_str = buf.getvalue().decode("utf-8")
print(filecontent_str)
Don’t forget to fill in MY_ACCESS_KEY
and MY_SECRET_KEY
. Depending on what region and what S3-compatible service you use, you might need to use another endpoint URL instead of https://s3.eu-central-1.wasabisys.com
.