AWS Command Line Interface (AWS CLI)

AWS CLI is a command line interface designed for AWS services. To learn how to run commands, see the official Amazon documentation.

To work with Yandex Object Storage via the AWS CLI, you can use the following sets of commands:

  • s3api — commands corresponding to operations in the REST API. Before you start, review the list of supported operations.
  • s3 — additional commands that make it easier to work with a large number of objects.

Before you start

  1. Create a service account.
  2. Assign a role to a service account.
  3. Create a static access key.

Installation

To install the AWS CLI, follow the guidelines on the developer's website.

Configuration

To configure the AWS CLI, use the aws configure command. The command will request values for the following parameters:

  1. AWS Access Key ID: enter the ID of the key that you received when generating the static key.

  2. AWS Secret Access Key: enter the secret key that you received when generating the static key.

  3. Default region name: Enter ru-central1.

    Note

    To work with Yandex Object Storage, always specify the ru-central1 region. A different value of the region may lead to an authorization error.

  4. Leave the other parameter values unchanged.

Configuration files

The aws configure command will result in saving your settings to the following files:

  • Your static key to the .aws/credentials file in the format:

    [default]
                aws_access_key_id = id
                aws_secret_access_key = secretKey
    
  • The default region to the .aws/config file in the format:

    [default]
                region=ru-central1
    

Specifics

When using the AWS CLI to work with Object Storage, keep the following in mind:

  • The AWS CLI treats Object Storage like a hierarchical file system and object keys look like file paths.

  • When running the aws command to work with Object Storage, the --endpoint-url parameter is required because the client is configured to work with the Amazon servers by default.

  • When using macOS, in some cases you need to run the command:

    export PYTHONPATH=/Library/Python/2.7/site-packages; aws --endpoint-url=https://storage.yandexcloud.net s3 ls
    

Examples of operations

Create a bucket

aws --endpoint-url=https://storage.yandexcloud.net s3 mb s3://bucket-name

Note

When creating a bucket, follow the naming conventions.

Uploading objects

You can upload objects using one of the following methods:

  • Upload all objects from a local directory:

    aws --endpoint-url=https://storage.yandexcloud.net \
        s3 cp --recursive local_files/ s3://bucket-name/path_style_prefix/
    
  • Upload objects specified in the --include filter and skip objects specified in the --exclude filter:

    aws --endpoint-url=https://storage.yandexcloud.net \
        s3 cp --recursive --exclude "*" --include "*.log" \
        local_files/ s3://bucket-name/path_style_prefix/
    
  • Upload objects one by one, running the following command for each object:

    aws --endpoint-url=https://storage.yandexcloud.net \
        s3 cp testfile.txt s3://bucket-name/path_style_prefix/textfile.txt
    

Getting a list of objects

aws --endpoint-url=https://storage.yandexcloud.net \
    s3 ls --recursive s3://bucket-name

Deleting objects

You can delete objects using one of the following methods:

  • Delete all objects with the specified prefix:

    aws --endpoint-url=https://storage.yandexcloud.net \
        s3 rm s3://bucket-name/path_style_prefix/ --recursive
    
  • Delete objects specified in the --include filter and skip objects specified in the --exclude filter:

    aws --endpoint-url=https://storage.yandexcloud.net \
        s3 rm s3://bucket-name/path_style_prefix/ --recursive \
        --exclude "*" --include "*.log"
    
  • Delete objects one by one, running the following command for each object:

    aws --endpoint-url=https://storage.yandexcloud.net \
        s3 rm s3://bucket-name/path_style_prefix/textfile.txt
    

Retrieve an object

aws --endpoint-url=https://storage.yandexcloud.net \
    s3 cp s3://bucket-name/textfile.txt textfile.txt