Skip to main content

AWS CLI for S3

Last update:

AWS CLI (AWS Command Line Interface) is a command-line interface for working with AWS services.

Configure AWS CLI

  1. Configure access to S3.
  2. Install the client.
  3. Create an AWS CLI configuration.
  4. Install a certificate.

1. Configure access to S3

Access can be configured by the Account Owner or a user with the iam.admin role.

  1. Create a service user with a role with S3 access. If you use a service user with the s3.user, object_storage_user, or s3.bucket.user role, an access policy must be configured for the bucket, and its rules must allow access to this user.
  2. Issue an S3 key to the user.

2. Install the client

Use the Install or update to the latest version of the AWS CLI section of the Amazon documentation.

3. Create an AWS CLI configuration

  1. Open the terminal.

  2. Open configuration mode:

    aws configure
  3. Enter AWS Access Key ID — the value of the Access key field from the S3 key.

  4. Enter AWS Secret Access Key — the value of the Secret key field from the S3 key.

  5. Enter Default region name — the pool where S3 is located (for example, ru-1).

  6. Enter Default output format or leave it blank.

  7. Settings will be saved in the configuration files:

    • credentials in .aws/credentials:

      [default]
      aws_access_key_id = <access_key>
      aws_secret_access_key = <secret_key>
    • the default pool in .aws/config:

      [default]
      region = <pool>
  8. In the .aws/config configuration file, add the endpoint_url: parameter:

    [default]
    region = <pool>
    endpoint_url = https://<s3_domain>

    Specify <s3_domain> — the S3 API domain for the required pool.

4. Install a certificate

  1. Создайте папку ~/.selectels3/:

    mkdir -p ~/.selectels3/
  2. Скачайте сертификат and поместите его in папку ~/.selectels3/:

    wget https://secure.globalsign.net/cacert/root-r6.crt -O ~/.selectels3/root.crt
    openssl x509 -inform der -in ~/.selectels3/root.crt -out ~/.selectels3/root.crt
    chmod 600 ~/.selectels3/root.crt
  3. In the .aws/config configuration file, add the parameter:

    ca_bundle = ~/.selectels3/root.crt

Working with AWS CLI

For command syntax, see the AWS section of the Amazon documentation.

To work with S3 via AWS CLI, use:

  • s3api — commands that correspond to REST API operations;
  • s3 — additional commands that simplify working with a large number of objects.

List buckets

  1. Open the CLI.

  2. List the buckets:

    aws s3 ls

Create a bucket

  1. Open the CLI.

  2. Create a bucket:

    aws s3 mb s3://<bucket_name>

    Specify <bucket_name> — the name of the new bucket.

List objects

  1. Open the CLI.

  2. List the objects:

    aws s3 ls --recursive s3://<bucket_name>

    Specify <bucket_name> — the name of the bucket to list objects in.

Upload an object

  1. Open the CLI.

  2. Upload an object to the bucket:

    aws s3 cp <path_to_object> s3://<bucket_name>/

    Specify:

    • <path_to_object> — the path in the bucket where the object will be stored;
    • <bucket_name> — the name of the bucket where the object will be stored.

Вы можете получить ссылку on объект in публичном or приватном бакете через подписанный URL (Presigned URL). Подробнее о Presigned URLs in инструкции Sharing objects with presigned URLs документации AWS.

  1. Open the CLI.

  2. Get a link:

    aws s3 presign s3://<bucket_name>/<path_to_object> --expires-in <time>

    Specify:

    • <bucket_name> — имя бакета, in котором хранится объект;
    • <path_to_object> — путь к объекту in бакете;
    • optional: --expires-in <time> — срок действия ссылки, где <time> — время in секундах, через которое ссылка перестанет работать. Если не добавить параметр --expires-in <time>, ссылка будет работать один час.

Copy an object

  1. Open the CLI.

  2. Copy the object:

    aws s3 cp s3://<bucket_name_1>/<path_to_object_1> s3://<bucket_name_2>/<path_to_object_2>

    Specify:

    • <bucket_name_1> — имя бакета, in котором хранится объект для копирования;
    • <path_to_object_1> — путь к объекту для копирования in бакете;
    • <bucket_name_2> — имя бакета, in который будет скопирован объект;
    • <path_to_object_2> — путь in бакете, по которому будет храниться объект.

Delete an object

  1. Open the CLI.

  2. Delete the object:

    aws s3 rm s3://<bucket_name>/<object_name>

    Specify:

    • <bucket_name> — имя бакета;
    • <object_name> — имя объекта.