How to download Wasabi/S3 object to file using boto3 in Python
You can use boto3’s download_fileobj()
in order to download files from S3 to the local filesystem:
with open("myfile.txt", "wb") as outfile:
my_bucket.download_fileobj("myfile.txt", outfile)
Note that the file needs to be opened in binary mode ("wb"
).
Full example
import boto3
# 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 remote object "myfile.txt" to local file "test.txt"
my_bucket.download_file("myfile.txt", "test.txt")
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
.