# Use AWS CLI and s5cmd

> Connect to Bitiful S4 with AWS CLI and s5cmd: configuration, upload and download, sync, presigned links, move, and batch operations.

AWS CLI is the official command-line tool and the most complete; s5cmd handles many files with high concurrency and is faster. Both can connect to S4 directly.

## Prerequisites

- You have [created a bucket](https://docs.bitiful.com/en/bitiful-s4/handbook/buckets.md);
- You have [created a sub-user and AccessKey](https://docs.bitiful.com/en/bitiful-s4/handbook/access-keys.md) and completed the [grants](https://docs.bitiful.com/en/bitiful-s4/handbook/permissions.md).

## AWS CLI

### Install and configure

Install by following the [official instructions](https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/getting-started-install.html), then create a separate profile for S4:

```bash
aws configure set aws_access_key_id "Your AccessKey" --profile s4
aws configure set aws_secret_access_key "Your SecretKey" --profile s4
aws configure set region cn-east-1 --profile s4
aws configure set endpoint_url https://s3.bitiful.net --profile s4
```

Putting `endpoint_url` in a profile requires AWS CLI 2.13 or later. With older versions, add `--endpoint-url https://s3.bitiful.net` to every command.

Add `--profile s4` to all later commands, or set the environment variable first:

```bash
export AWS_PROFILE=s4
```

### Upload and download

```bash
# Upload
aws s3 cp ./logo.png s3://my-bucket/images/logo.png

# Specify the type and browser cache
aws s3 cp ./app.js s3://my-bucket/static/app.js \
  --content-type "application/javascript" --cache-control "max-age=86400"

# Upload an entire directory
aws s3 cp ./dist s3://my-bucket/site/ --recursive

# Download
aws s3 cp s3://my-bucket/images/logo.png ./logo.png
```

### Sync

```bash
# Upload only files that changed
aws s3 sync ./dist s3://my-bucket/site/

# Also delete extra files in the bucket; do a dry run with --dryrun first
aws s3 sync ./dist s3://my-bucket/site/ --delete --dryrun
aws s3 sync ./dist s3://my-bucket/site/ --delete
```

### View and delete

```bash
aws s3 ls                                  # Buckets you have been granted
aws s3 ls s3://my-bucket/images/           # Top-level directory
aws s3 ls s3://my-bucket/images/ --recursive --human-readable --summarize

aws s3 rm s3://my-bucket/images/logo.png
aws s3 rm s3://my-bucket/tmp/ --recursive  # Delete an entire directory; cannot be undone
```

### Presigned download links

```bash
aws s3 presign s3://my-bucket/images/logo.png --expires-in 3600
```

The validity period is in seconds, up to 604800 (7 days).

### Move and conditional writes

```bash
# Native move, no data copied
aws s3api copy-object --bucket my-bucket --key "new/photo.jpg" \
  --copy-source "my-bucket/old/photo.jpg?move=true"

# Write only if the object does not exist
aws s3api put-object --bucket my-bucket --key locks/job --body ./job.txt --if-none-match "*"
```

`aws s3 mv` copies and then deletes, so use the move above for large files. `--if-none-match` requires a recent AWS CLI.

### Adjust concurrency and part size

```bash
aws configure set s3.max_concurrent_requests 20 --profile s4
aws configure set s3.multipart_threshold 64MB --profile s4
aws configure set s3.multipart_chunksize 64MB --profile s4
```

## s5cmd

### Install and configure

Download it from the [s5cmd releases page](https://github.com/peak/s5cmd/releases), or install it with Homebrew:

```bash
brew install peak/tap/s5cmd
```

Configure it with environment variables:

```bash
export AWS_ACCESS_KEY_ID="Your AccessKey"
export AWS_SECRET_ACCESS_KEY="Your SecretKey"
export AWS_REGION=cn-east-1
export S3_ENDPOINT_URL=https://s3.bitiful.net
```

### Common commands

```bash
# List
s5cmd ls s3://my-bucket/images/

# Upload a directory
s5cmd cp './dist/*' s3://my-bucket/site/

# Download a directory
s5cmd cp 's3://my-bucket/site/*' ./dist/

# Sync
s5cmd sync ./dist/ s3://my-bucket/site/

# Batch delete
s5cmd rm 's3://my-bucket/tmp/*'

# Calculate size
s5cmd du --humanize 's3://my-bucket/site/*'
```

Adjust concurrency:

```bash
s5cmd --numworkers 64 cp './data/*' s3://my-bucket/data/
```

The default concurrency is 256, which can trigger the [QPS limit](https://docs.bitiful.com/en/bitiful-s4/limits.md); lower it when you see 429.

## FAQ

<details>
<summary>aws s3 mb returns 403</summary>

Sub-users cannot create buckets. Create buckets in the [console](https://docs.bitiful.com/en/bitiful-s4/handbook/buckets.md).

</details>

<details>
<summary>aws s3 ls returns nothing</summary>

It only lists buckets the sub-user has been granted access to. [Grant access](https://docs.bitiful.com/en/bitiful-s4/handbook/permissions.md) first.

</details>

<details>
<summary>Could not connect to the endpoint URL</summary>

The endpoint is not set, so the request went to AWS. Check with `aws configure list --profile s4`, or add `--endpoint-url` to the command.

</details>

<details>
<summary>SignatureDoesNotMatch</summary>

Check whether the keys have extra spaces, whether the Region is `cn-east-1`, and whether the local clock is accurate.

</details>
