Skip to main content
Last updated on

AWS CLI

AWS CLI (Amazon Web Services Command Line Interface) is the official AWS command-line tool, fully compatible with LANIT Cloud Simple Storage via the S3-compatible protocol. It is the ideal choice for DevOps, automation scripts, and CI/CD pipeline integration.

Installing AWS CLI

Linux

# Ubuntu / Debian
sudo apt update && sudo apt install awscli

# Or install the latest version (v2)
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

macOS

# Homebrew
brew install awscli

# Or download the .pkg package from the AWS website

Windows

Download the .msi installer from https://aws.amazon.com/cli/ and run it.

Verify the installation:

aws --version
# aws-cli/2.x.x Python/3.x.x ...

Configuring the LANIT Cloud Connection

First, get your Access Key ID and Secret Access Key from the LANIT Cloud Portal (see Access Key):

Access Key management page on LANIT Cloud Portal

aws configure --profile lanit

Enter each value when prompted:

AWS Access Key ID [None]: YOUR_ACCESS_KEY_ID
AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY
Default region name [None]: hn
Default output format [None]: json

Method 2: Editing the configuration file directly

Open ~/.aws/credentials (Linux/macOS) or %USERPROFILE%\.aws\credentials (Windows):

[lanit]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

Open ~/.aws/config:

[profile lanit]
region = hn
output = json

Setting up the endpoint

LANIT Cloud Simple Storage is not AWS, so every command requires the --endpoint-url parameter. Create a shell alias for convenience:

Linux/macOS — add to ~/.bashrc or ~/.zshrc:

alias aws-lanit='aws --endpoint-url https://s3.lanit.com.vn --profile lanit'

Reload the shell:

source ~/.bashrc

Test the connection:

aws-lanit s3 ls

Common Commands

All examples below use the aws-lanit alias. If you are not using the alias, append --endpoint-url https://s3.lanit.com.vn --profile lanit to every command.

Bucket Management

# List all buckets
aws-lanit s3 ls

# Create a new bucket
aws-lanit s3 mb s3://new-bucket-name

# Delete an empty bucket
aws-lanit s3 rb s3://bucket-name

# Delete a bucket and all its contents
aws-lanit s3 rb s3://bucket-name --force

Object Management

# List objects in a bucket
aws-lanit s3 ls s3://bucket-name

# List recursively (including subfolders)
aws-lanit s3 ls s3://bucket-name --recursive

# List with file sizes
aws-lanit s3 ls s3://bucket-name --recursive --human-readable --summarize

Upload and Download

# Upload a single file
aws-lanit s3 cp /path/to/local/file.txt s3://bucket-name/

# Upload to a specific folder inside the bucket
aws-lanit s3 cp report.pdf s3://bucket-name/reports/2024/

# Upload an entire directory
aws-lanit s3 cp /local/folder/ s3://bucket-name/folder/ --recursive

# Download a file to your machine
aws-lanit s3 cp s3://bucket-name/file.txt /local/downloads/

# Download an entire bucket to a local directory
aws-lanit s3 cp s3://bucket-name/ /local/backup/ --recursive

Synchronization (Sync)

# Sync local → bucket (only uploads new or changed files)
aws-lanit s3 sync /local/folder/ s3://bucket-name/

# Preview (dry run)
aws-lanit s3 sync /local/folder/ s3://bucket-name/ --dryrun

# Sync bucket → local
aws-lanit s3 sync s3://bucket-name/ /local/folder/

# Delete destination files that no longer exist in the source
aws-lanit s3 sync /local/folder/ s3://bucket-name/ --delete

# Exclude files by pattern
aws-lanit s3 sync /local/folder/ s3://bucket-name/ --exclude "*.log" --exclude "tmp/*"

# Sync only files matching a pattern
aws-lanit s3 sync /local/folder/ s3://bucket-name/ --include "*.jpg" --include "*.png"

Deleting Objects

# Delete a single file
aws-lanit s3 rm s3://bucket-name/file.txt

# Delete all contents of a folder
aws-lanit s3 rm s3://bucket-name/old-logs/ --recursive

# Delete by pattern
aws-lanit s3 rm s3://bucket-name/ --recursive --exclude "*" --include "*.tmp"

Advanced Operations with s3api

The s3api command allows more granular operations than s3:

# View object metadata
aws-lanit s3api head-object \
--bucket bucket-name \
--key path/to/object.pdf

# Generate a presigned URL (valid for 1 hour)
aws-lanit s3 presign s3://bucket-name/private-file.pdf \
--expires-in 3600

# Get bucket information
aws-lanit s3api get-bucket-location --bucket bucket-name

# Read the Bucket Policy
aws-lanit s3api get-bucket-policy --bucket bucket-name

# Set a Bucket Policy from a JSON file
aws-lanit s3api put-bucket-policy \
--bucket bucket-name \
--policy file://policy.json

# Get the CORS configuration
aws-lanit s3api get-bucket-cors --bucket bucket-name

# Set the CORS configuration from a JSON file
aws-lanit s3api put-bucket-cors \
--bucket bucket-name \
--cors-configuration file://cors.json

High-Performance Upload (Multipart)

AWS CLI automatically uses multipart upload for files larger than 8 MB. To customize:

aws-lanit s3 cp large-file.iso s3://bucket-name/ \
--expected-size 10737418240 \
--part-size 32mb \
--acl private

Or set defaults in ~/.aws/config:

[profile lanit]
region = hn
s3 =
multipart_threshold = 64MB
multipart_chunksize = 32MB
max_concurrent_requests = 10

Using in Scripts / CI-CD

Example automated backup script:

#!/bin/bash
BACKUP_DIR="/var/data"
BUCKET="bucket-name"
DATE=$(date +%Y%m%d-%H%M%S)
ENDPOINT="https://s3.lanit.com.vn"

# Compress and upload
tar -czf /tmp/backup-${DATE}.tar.gz ${BACKUP_DIR}
aws s3 cp /tmp/backup-${DATE}.tar.gz s3://${BUCKET}/backups/ \
--endpoint-url ${ENDPOINT} \
--profile lanit

# Clean up temporary file
rm /tmp/backup-${DATE}.tar.gz

echo "Backup complete: backup-${DATE}.tar.gz"

Environment Variables

Instead of using configuration files, you can pass credentials via environment variables (suitable for Docker, CI/CD):

export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY
export AWS_DEFAULT_REGION=hn

aws s3 ls --endpoint-url https://s3.lanit.com.vn

Troubleshooting

Error An error occurred (InvalidAccessKeyId):

  • Verify that your Access Key ID and Secret Access Key are correct and Active.
  • Check which profile is in use: aws configure list --profile lanit

Error An error occurred (SignatureDoesNotMatch):

  • Make sure the endpoint is correct: https://s3.lanit.com.vn
  • Verify that your system clock is accurate.

Error SSL certificate verify failed:

  • Add the --no-verify-ssl flag (for debugging only — do not use in production).
  • Or update CA certificates: sudo update-ca-certificates

Very slow uploads:

  • Increase concurrent requests in config: max_concurrent_requests = 20
  • Increase the chunk size: multipart_chunksize = 64MB