# Boto3 Python SDK

[**Boto3**](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html) 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**](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html), 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**](https://docs.ionos.com/cloud/getting-started/data-center-designer). [Log in to the DCD](https://docs.ionos.com/cloud/getting-started/basic-tutorials/log-in-dcd), click on the **Storage > S3 Key Management** to get the Object Storage Keys.

**NOTE:** Your credentials are not tied to a specific region or bucket.

{% hint style="info" %}
For information on the supported IONOS S3 Object Storage Service endpoints, see [**Endpoints**](https://docs.ionos.com/cloud/~/revisions/1AlFSZBNMIBgs7fiOwFD/managed-services/s3-object-storage/endpoints).
{% endhint %}

```python
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:

  ```python
  response = s3_client.list_buckets()
  for bucket in response['Buckets']:
      print(bucket['Name'])
  ```
* Create bucket `my-bucket` at the region `eu-central-1`:

  ```python
  try:
    s3_client.create_bucket(Bucket='my-bucket', CreateBucketConfiguration={'LocationConstraint': 'eu-central-2'})
  except ClientError as e:
          logging.error(e)
  ```
* Upload **filename.txt** to the bucket `my-bucket`:

  ```python
  try:
          s3_client.upload_file(
              Filename='filename.txt',
              Bucket='my-bucket',
              Key='my-prefix/filename.txt')
  except ClientError as e:
          logging.error(e)
  ```

  For more information, see AWS SDK documentation on [Uploading files](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html).
* Download the file `filename.txt` from the `my-bucket`:

  ```python
  s3_client.download_file('my-bucket',
                       'filename.txt',
                       '/local_path/filename.txt')
  ```
* List objects of the bucket `my-bucket`

  ```python
  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 bucket `my-dest-bucket` and add the prefix `uploaded/`. Instead of the `client()` method, we use the `resource()` method here. It provides a higher level of abstraction than the low-level calls made by service clients.

  ```python
  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)   
  ```

For more examples, see [Boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html), such as:

* [Generating presigned URLs](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html)
* [Set a bucket policy](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-bucket-policies.html)
* [Set a bucket CORS configuration](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-configuring-buckets.html)

For more information on Boto3 and Python, see [Realpython.com – Python, Boto3, and AWS S3: Demystified](https://realpython.com/python-boto3-aws-s3/).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ionos.com/cloud/~/revisions/1AlFSZBNMIBgs7fiOwFD/managed-services/s3-object-storage/s3-tools/boto3-python-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
