# AWS CLI

IONOS S3 Object Storage supports using Amazon's AWS Command Line Interface (AWS CLI) for Windows, macOS, and Linux.

For the installation instructions, see [**Installing or updating the latest version of the AWS CLI**](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).

### Configuration

1. Run the following command in a terminal: `aws configure`.
2. **AWS Access Key ID \[None]**: Insert the Access Key. To get it, [log in to the DCD](https://docs.ionos.com/cloud/getting-started/basic-tutorials/log-in-dcd), go to **Menu** > **Storage** > **IONOS S3 Object Storage** > **Key management**.
3. **AWS Secret Access Key \[None]**: Paste the Secret Key. It can be found in the Data Center Designer by selecting **Storage** > **S3 Key Management**.
4. **Default region name \[None]**: `de`.
5. **Default output format \[None]**: `json`.

### Using AWS CLI with IONOS S3 Object Storage

For each command, be sure to include one of the endpoints in the `endpoint-url` parameter:

```
--endpoint-url https://s3-eu-central-2.ionoscloud.com
```

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

There are 2 sets of commands:

* [**s3**](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/index.html): Offers high-level commands for managing S3 buckets and for moving, copying, and synchronizing objects.
* [**s3api**](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/index.html): Allows you to work with specific features such as ACL, CORS, and Versioning.

#### Sample Usage

* List buckets:
  * Option 1: Using s3 set of commands

    ```
    aws s3 ls s3://my-bucket --endpoint-url https://s3-eu-central-2.ionoscloud.com
    ```
  * Option 2: Using s3api set of commands

    ```
    aws s3api list-buckets --endpoint-url https://s3-eu-central-2.ionoscloud.com
    ```
* Create a bucket in the `eu-central-2` region (Berlin, Germany):
  * Option 1: Using s3 set of commands

    ```
    aws s3 mb s3://my-bucket --region eu-central-2 --endpoint-url https://s3-eu-central-2.ionoscloud.com
    ```
  * Option 2: Using s3api set of commands

    ```
    aws s3api create-bucket --bucket my-bucket --region=eu-central-2 --create-bucket-configuration LocationConstraint=eu-central-2 --endpoint-url https://s3-eu-central-2.ionoscloud.com
    ```
* Create a bucket in the `de` region (Frankfurt, Germany) with Object Lock enabled:

  ```
  aws s3api create-bucket --bucket my-bucket --object-lock-enabled-for-bucket --region=de --create-bucket-configuration LocationConstraint=de --endpoint-url https://s3-eu-central-1.ionoscloud.com
  ```
* Upload an object from the current directory to a bucket:

  ```
  aws s3 cp filename.txt s3://my-bucket --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```
* Copy the object to the bucket:

  ```
  aws s3 cp my-dir s3://my-bucket/ --recursive --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```
* Copy the contents of the local directory `my-dir` to the bucket `my-bucket`:

  ```
  aws s3 cp my-dir s3://my-bucket/ --recursive --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```

  For more information, see [cp command reference](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/cp.html).
* Copy all objects from `my-source-bucket` to `my-dest-bucket` excluding .zip files. The command doesn’t support cross-region copying for IONOS S3 Object Storage:

  ```
  aws s3 cp s3://my-source-bucket/ s3://my-dest-bucket/ --recursive --exclude "*.zip" --endpoint-url https://s3-eu-central-2.ionoscloud.com`
  ```
* Download all the objects from the `my-bucket` bucket to the local directory `my-dir`:

  ```
  aws s3 cp s3://my-bucket my-dir --recursive --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```
* Sync the bucket `my-bucket` with the contents of the local directory `my-dir`:

  ```
  aws s3 sync my-dir s3://my-bucket --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```

  For more information, see [sync command reference](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/sync.html).
* Get Cross-Origin Resource Sharing (CORS) configuration:

  ```
  aws s3api get-bucket-cors --bucket my-bucket --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```
* Set up Cross-Origin Resource Sharing (CORS) configuration:

  ```
  aws s3api put-bucket-cors --bucket my-bucket --cors-configuration file://cors.json --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```

  **cors.json:**

  ```json
  {
    "CORSRules": [
      {
        "AllowedOrigins": ["http://www.example.com"],
        "AllowedHeaders": ["*"],
        "AllowedMethods": ["PUT", "POST", "DELETE"],
        "MaxAgeSeconds": 3000,
        "ExposeHeaders": ["x-amz-server-side-encryption"]
      },
      {
        "AllowedOrigins": ["*"],
        "AllowedHeaders": ["Authorization"],
        "AllowedMethods": ["GET"],
        "MaxAgeSeconds": 3000
      }
    ]
  }
  ```

  For more information, see [put-bucket-cors command reference](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/put-bucket-cors.html).
* Enable versioning for the bucket:

  ```
  aws s3api put-bucket-versioning --bucket my-bucket --versioning-configuration Status=Enabled --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```
* Get versioning state of the bucket:

  ```
  aws s3api get-bucket-versioning --bucket my-bucket --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```
* Set up a lifetime policy for a bucket (delete objects starting with "my/prefix/" older than 5 days):

  ```
  aws s3api put-bucket-lifecycle-configuration --bucket my-bucket  --lifecycle-configuration file://delete-after-5-days.json --endpoint-url https://s3-eu-central-2.ionoscloud.com
  ```

  **delete-after-5-days.json:**

  ```json
  {
      "Rules":
      [
          {
              "ID" : "Delete older than 5 days",
              "Expiration" :
              {
                  "Days": 5
              },
              "Filter": {
                  "Prefix": "my/prefix/"
              },
              "Status" : "Enabled"
          }
      ]
  }
  ```
