AWS CLI in S3
AWS CLI (AWS Command Line Interface) is a command-line interface for working with AWS services.
Configure AWS CLI
- Configure access to S3.
- Install the client.
- Create an AWS CLI configuration.
- Install the certificate.
1. Configure access to S3
Access can be configured by the Account Owner or a user with the iam.admin role.
- Create a service user with a role with access to S3. If you use a service user with the
s3.user,object_storage_userors3.bucket.userrole, the bucket must have a configured access policy and its rules must allow access to this user. - Issue an S3 key to the user.
2. Install the client
Use the Install or update to the latest version of the AWS CLI guide in the Amazon documentation.
3. Create an AWS CLI configuration
-
Open the terminal.
-
Open configuration mode:
aws configure -
Enter
AWS Access Key ID— the value of the Access key field from the S3 key. -
Enter
AWS Secret Access Key— the value of the Secret key field from the S3 key. -
Enter
Default region name— the pool where S3 is located (for example,ru-1). -
Enter
Default output formator leave it blank. -
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> -
default pool in
.aws/config:[default]region = <pool>
-
-
In the
.aws/configconfiguration file, add theendpoint_urlparameter:[default]region = <pool>endpoint_url = https://<s3_domain>Specify
<s3_domain>— the S3 API domain for the required pool.
4. Install a certificate
Linux/macOS
Windows
-
Create the
~/.servercores3/folder:mkdir -p ~/.servercores3/ -
Download the certificate and place it in the
~/.servercores3/folder:wget https://secure.globalsign.net/cacert/root-r6.crt -O ~/.servercores3/root.crtopenssl x509 -inform der -in ~/.servercores3/root.crt -out ~/.servercores3/root.crtchmod 600 ~/.servercores3/root.crt -
Add the parameter to the
.aws/configconfiguration file:ca_bundle = ~/.servercores3/root.crt
Working with AWS CLI
For command syntax, see the AWS guide in the Amazon documentation.
To work with S3 via AWS CLI, use:
- s3api — commands corresponding to operations in the REST API;
- s3 — additional commands that simplify working with a large number of objects.
List buckets
-
Open the CLI.
-
List the buckets:
aws s3 ls
Create a bucket
-
Open the CLI.
-
Create a bucket:
aws s3 mb s3://<bucket_name>Specify
<bucket_name>— the name of the new bucket.
List objects
-
Open the CLI.
-
List the objects:
aws s3 ls --recursive s3://<bucket_name>Specify
<bucket_name>— the name of the bucket where you want to view the list of objects.
Upload an object
Simple upload
Upload with conditional request
Upload with Object Lock
-
Open the CLI.
-
Upload an object to a bucket:
aws s3 cp <path_to_object> s3://<bucket_name>/Specify:
<path_to_object>— path in the bucket where the object will be stored;<bucket_name>— name of the bucket where the object will be stored.
Get a link to an object
You can get a link to an object in a public or private bucket via a signed URL (Presigned URL). For more information about Presigned URLs, see the Sharing objects with presigned URLs guide in the AWS documentation.
-
Open the CLI.
-
Get the link:
aws s3 presign s3://<bucket_name>/<path_to_object> --expires-in <time>Specify:
<bucket_name>— name of the bucket where the object is stored;<path_to_object>— path to the object in the bucket;- optional:
--expires-in <time>— link expiration time, where<time>is the time in seconds after which the link will expire. If you do not specify the--expires-in <time>parameter, the link will be valid for one hour.
Copy an object
Simple copy
Copy with condition
-
Open the CLI.
-
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>— name of the bucket where the object to copy is stored;<path_to_object_1>— path to the object to copy in the bucket;<bucket_name_2>— name of the bucket to which the object will be copied;<path_to_object_2>— path in the bucket where the object will be stored.
Delete an object
Simple deletion
Deletion with condition
-
Open the CLI.
-
Delete the object:
aws s3 rm s3://<bucket_name>/<object_name>Specify:
<bucket_name>— bucket name;<object_name>— object name.