Boto3 Python SDK
Boto3 is the official AWS SDK for Python. It allows you to create, update, and configure IONOS S3 Object Storage objects from within your Python scripts.
Configuration
Install the latest Boto3 release via pip: pip install boto3
There are several ways to provide credentials, e.g. passing credentials as parameters to the boto.client() method, via environment variables, or with a generic credential file (~/.aws/credentials).
An example of passing credentials as parameters when creating a Session object:
Your Access and Secret keys can be obtained from the Data Center Designer: click on the Manager resources > Object Storage Key Manager. Your credentials are not tied to a specific region or bucket. Please refer to the list of regions and endpoints of IONOS S3 Object Storage.
import boto3
import logging
from botocore.exceptions import ClientError
from botocore.client import Config
config = Config(
signature_version = 's3v4'
)
s3_client = boto3.client('s3',
endpoint_url='https://s3-eu-central-2.ionoscloud.com',
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
config=config)
Sample Usage
List buckets:
response = s3_client.list_buckets()
for bucket in response['Buckets']:
print(bucket['Name'])
Create bucket
my-bucket
at the regioneu-central-1
:
try:
s3_client.create_bucket(Bucket='my-bucket', CreateBucketConfiguration={'LocationConstraint': 'eu-central-1'})
except ClientError as e:
logging.error(e)
Upload filename.txt to the bucket
my-bucket
(see more examples):
try:
s3_client.upload_file(
Filename='filename.txt',
Bucket='my-bucket',
Key='my-prefix/filename.txt')
except ClientError as e:
logging.error(e)
Download the file
filename.txt
from themy-bucket
:
s3_client.download_file('my-bucket',
'filename.txt',
'/local_path/filename.txt')
List objects of the bucket
my-bucket
response = s3_client.list_objects(Bucket='my-bucket')
for obj in response['Contents']:
print(obj['Key'])
Copy the filename.txt from the bucket
my-source-bucket
to the bucketmy-dest-bucket
and add the prefixuploaded/
. Instead of theclient()
method, we use theresource()
method here. It provides a higher level of abstraction than the low-level calls made by service clients.
config = Config(
signature_version = 's3v4'
)
s3_resource = boto3.resource('s3',
endpoint_url='https://s3-eu-central-2.ionoscloud.com',
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
config=config)
copy_source = {
'Bucket': 'my-source-bucket',
'Key': 'filename.txt'
}
bucket = s3_resource.Bucket('my-dest-bucket')
try:
bucket.copy(copy_source, 'uploaded/filename.txt')
except ClientError as e:
logging.error(e)
More samples could be found here:
Additional resources:
Last updated
Was this helpful?