> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.bitiful.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.bitiful.com/_mcp/server.

# 使用 AWS CLI 与 s5cmd

> 用 AWS CLI 和 s5cmd 连接缤纷云 S4：配置、上传下载、同步、预签名链接、移动与批量操作。

AWS CLI 是官方命令行工具，功能最全；s5cmd 用高并发处理大量文件，速度更快。两者都可以直接连接 S4。

## 前置条件

- 已 [创建存储桶](https://docs.bitiful.com/bitiful-s4/handbook/buckets.md)；
- 已 [创建子账户与 AccessKey](https://docs.bitiful.com/bitiful-s4/handbook/access-keys.md)，并完成 [授权](https://docs.bitiful.com/bitiful-s4/handbook/permissions.md)。

## AWS CLI

### 安装与配置

按 [官方说明](https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/getting-started-install.html) 安装，然后为 S4 单独建一个 profile：

**Windows**

```powershell title="PowerShell"
aws.exe configure set aws_access_key_id "你的 AccessKey" --profile s4
aws.exe configure set aws_secret_access_key "你的 SecretKey" --profile s4
aws.exe configure set region cn-east-1 --profile s4
aws.exe configure set endpoint_url https://s3.bitiful.net --profile s4
```

**macOS**

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

**Linux**

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

`endpoint_url` 写进 profile 需要 AWS CLI 2.13 以上。更旧的版本，每条命令都要加 `--endpoint-url https://s3.bitiful.net`。

之后的命令都加上 `--profile s4`，或先设置环境变量：

**Windows**

```powershell title="PowerShell"
$env:AWS_PROFILE="s4"
```

**macOS**

```bash
export AWS_PROFILE=s4
```

**Linux**

```bash
export AWS_PROFILE=s4
```

Windows 的命令请在 PowerShell 里运行，示例里的 `D:\` 换成自己的目录。

### 上传与下载

**Windows**

```powershell title="PowerShell"
# 上传
aws.exe s3 cp D:\logo.png s3://my-bucket/images/logo.png

# 指定类型和浏览器缓存
aws.exe s3 cp D:\app.js s3://my-bucket/static/app.js --content-type "application/javascript" --cache-control "max-age=86400"

# 上传整个目录
aws.exe s3 cp D:\dist s3://my-bucket/site/ --recursive

# 只上传图片
aws.exe s3 cp D:\assets s3://my-bucket/assets/ --recursive --exclude "*" --include "*.png" --include "*.jpg"

# 下载
aws.exe s3 cp s3://my-bucket/images/logo.png D:\logo.png

# 备份数据库并上传
mysqldump.exe mydb --result-file=D:\backup\mydb.sql
aws.exe s3 cp D:\backup\mydb.sql s3://my-bucket/backup/mydb.sql

# 输出到标准输出
aws.exe s3 cp s3://my-bucket/notes/todo.txt -
```

**macOS**

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

# 指定类型和浏览器缓存
aws s3 cp ./app.js s3://my-bucket/static/app.js --content-type "application/javascript" --cache-control "max-age=86400"

# 上传整个目录
aws s3 cp ./dist s3://my-bucket/site/ --recursive

# 只上传图片
aws s3 cp ./assets s3://my-bucket/assets/ --recursive --exclude "*" --include "*.png" --include "*.jpg"

# 下载
aws s3 cp s3://my-bucket/images/logo.png ./logo.png

# 从标准输入上传，或输出到标准输出
mysqldump mydb | gzip | aws s3 cp - s3://my-bucket/backup/mydb.sql.gz
aws s3 cp s3://my-bucket/notes/todo.txt -
```

**Linux**

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

# 指定类型和浏览器缓存
aws s3 cp ./app.js s3://my-bucket/static/app.js --content-type "application/javascript" --cache-control "max-age=86400"

# 上传整个目录
aws s3 cp ./dist s3://my-bucket/site/ --recursive

# 只上传图片
aws s3 cp ./assets s3://my-bucket/assets/ --recursive --exclude "*" --include "*.png" --include "*.jpg"

# 下载
aws s3 cp s3://my-bucket/images/logo.png ./logo.png

# 从标准输入上传，或输出到标准输出
mysqldump mydb | gzip | aws s3 cp - s3://my-bucket/backup/mydb.sql.gz
aws s3 cp s3://my-bucket/notes/todo.txt -
```

### 同步

**Windows**

```powershell title="PowerShell"
# 只上传有变化的文件
aws.exe s3 sync D:\dist s3://my-bucket/site/

# 同时删除桶里多余的文件，先用 --dryrun 预演
aws.exe s3 sync D:\dist s3://my-bucket/site/ --delete --dryrun
aws.exe s3 sync D:\dist s3://my-bucket/site/ --delete

# CI 里每次构建的文件时间都会变，按大小比较可以少传
aws.exe s3 sync D:\dist s3://my-bucket/site/ --size-only
```

**macOS**

```bash
# 只上传有变化的文件
aws s3 sync ./dist s3://my-bucket/site/

# 同时删除桶里多余的文件，先用 --dryrun 预演
aws s3 sync ./dist s3://my-bucket/site/ --delete --dryrun
aws s3 sync ./dist s3://my-bucket/site/ --delete

# CI 里每次构建的文件时间都会变，按大小比较可以少传
aws s3 sync ./dist s3://my-bucket/site/ --size-only
```

**Linux**

```bash
# 只上传有变化的文件
aws s3 sync ./dist s3://my-bucket/site/

# 同时删除桶里多余的文件，先用 --dryrun 预演
aws s3 sync ./dist s3://my-bucket/site/ --delete --dryrun
aws s3 sync ./dist s3://my-bucket/site/ --delete

# CI 里每次构建的文件时间都会变，按大小比较可以少传
aws s3 sync ./dist s3://my-bucket/site/ --size-only
```

### 查看与删除

**Windows**

```powershell title="PowerShell"
aws.exe s3 ls                                  # 有授权的桶
aws.exe s3 ls s3://my-bucket/images/           # 一级目录
aws.exe s3 ls s3://my-bucket/images/ --recursive --human-readable --summarize

# 用条件筛选，例如大于 100 MB 的对象
aws.exe s3api list-objects-v2 --bucket my-bucket --prefix images/ --query 'Contents[?Size>`104857600`].[Key,Size]' --output text

# 查看元数据
aws.exe s3api head-object --bucket my-bucket --key images/logo.png

aws.exe s3 rm s3://my-bucket/images/logo.png
aws.exe s3 rm s3://my-bucket/tmp/ --recursive  # 删除整个目录，无法恢复
```

**macOS**

```bash
aws s3 ls                                  # 有授权的桶
aws s3 ls s3://my-bucket/images/           # 一级目录
aws s3 ls s3://my-bucket/images/ --recursive --human-readable --summarize

# 用条件筛选，例如大于 100 MB 的对象
aws s3api list-objects-v2 --bucket my-bucket --prefix images/ --query 'Contents[?Size>`104857600`].[Key,Size]' --output text

# 查看元数据
aws s3api head-object --bucket my-bucket --key images/logo.png

aws s3 rm s3://my-bucket/images/logo.png
aws s3 rm s3://my-bucket/tmp/ --recursive  # 删除整个目录，无法恢复
```

**Linux**

```bash
aws s3 ls                                  # 有授权的桶
aws s3 ls s3://my-bucket/images/           # 一级目录
aws s3 ls s3://my-bucket/images/ --recursive --human-readable --summarize

# 用条件筛选，例如大于 100 MB 的对象
aws s3api list-objects-v2 --bucket my-bucket --prefix images/ --query 'Contents[?Size>`104857600`].[Key,Size]' --output text

# 查看元数据
aws s3api head-object --bucket my-bucket --key images/logo.png

aws s3 rm s3://my-bucket/images/logo.png
aws s3 rm s3://my-bucket/tmp/ --recursive  # 删除整个目录，无法恢复
```

### 预签名下载链接

**Windows**

```powershell title="PowerShell"
aws.exe s3 presign s3://my-bucket/images/logo.png --expires-in 3600
```

**macOS**

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

**Linux**

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

有效期单位是秒，最长 604800（7 天）。上传用的预签名链接和 S4 扩展参数见 [预签名 URL](https://docs.bitiful.com/developer/s3/presigned-url.md)。

### 元数据与标签

**Windows**

```powershell title="PowerShell"
# 修改类型和缓存头，不重新上传
aws.exe s3 cp s3://my-bucket/app.js s3://my-bucket/app.js --metadata-directive REPLACE --content-type "application/javascript" --cache-control "max-age=86400"

# 设置、查看标签
aws.exe s3api put-object-tagging --bucket my-bucket --key app.js --tagging 'TagSet=[{Key=env,Value=prod}]'
aws.exe s3api get-object-tagging --bucket my-bucket --key app.js
```

**macOS**

```bash
# 修改类型和缓存头，不重新上传
aws s3 cp s3://my-bucket/app.js s3://my-bucket/app.js --metadata-directive REPLACE --content-type "application/javascript" --cache-control "max-age=86400"

# 设置、查看标签
aws s3api put-object-tagging --bucket my-bucket --key app.js --tagging 'TagSet=[{Key=env,Value=prod}]'
aws s3api get-object-tagging --bucket my-bucket --key app.js
```

**Linux**

```bash
# 修改类型和缓存头，不重新上传
aws s3 cp s3://my-bucket/app.js s3://my-bucket/app.js --metadata-directive REPLACE --content-type "application/javascript" --cache-control "max-age=86400"

# 设置、查看标签
aws s3api put-object-tagging --bucket my-bucket --key app.js --tagging 'TagSet=[{Key=env,Value=prod}]'
aws s3api get-object-tagging --bucket my-bucket --key app.js
```

`REPLACE` 会替换全部元数据，没有写的自定义元数据会被清掉。

### 移动与条件写

**Windows**

```powershell title="PowerShell"
# 原生移动，不复制数据
aws.exe s3api copy-object --bucket my-bucket --key "new/photo.jpg" --copy-source "my-bucket/old/photo.jpg?move=true"

# 仅当对象不存在时写入
aws.exe s3api put-object --bucket my-bucket --key locks/job --body D:\job.txt --if-none-match "*"
```

**macOS**

```bash
# 原生移动，不复制数据
aws s3api copy-object --bucket my-bucket --key "new/photo.jpg" --copy-source "my-bucket/old/photo.jpg?move=true"

# 仅当对象不存在时写入
aws s3api put-object --bucket my-bucket --key locks/job --body ./job.txt --if-none-match "*"
```

**Linux**

```bash
# 原生移动，不复制数据
aws s3api copy-object --bucket my-bucket --key "new/photo.jpg" --copy-source "my-bucket/old/photo.jpg?move=true"

# 仅当对象不存在时写入
aws s3api put-object --bucket my-bucket --key locks/job --body ./job.txt --if-none-match "*"
```

`aws s3 mv` 是先复制再删除，大文件请用上面的移动，见 [移动/重命名对象](https://docs.bitiful.com/bitiful-s4/features/move-object.md)。对象已存在时，条件写返回 412 `PreconditionFailed`，见 [条件请求](https://docs.bitiful.com/developer/s3/conditional-requests.md)。`--if-none-match` 需要较新的 AWS CLI。

### 清理未完成的分片上传

中断的分片上传会继续占用空间：

**Windows**

```powershell title="PowerShell"
aws.exe s3api list-multipart-uploads --bucket my-bucket --query "Uploads[].[Key,UploadId,Initiated]" --output text

aws.exe s3api abort-multipart-upload --bucket my-bucket --key videos/a.mp4 --upload-id "上面的 UploadId"
```

**macOS**

```bash
aws s3api list-multipart-uploads --bucket my-bucket --query "Uploads[].[Key,UploadId,Initiated]" --output text

aws s3api abort-multipart-upload --bucket my-bucket --key videos/a.mp4 --upload-id "上面的 UploadId"
```

**Linux**

```bash
aws s3api list-multipart-uploads --bucket my-bucket --query "Uploads[].[Key,UploadId,Initiated]" --output text

aws s3api abort-multipart-upload --bucket my-bucket --key videos/a.mp4 --upload-id "上面的 UploadId"
```

### 调整并发与分片

**Windows**

```powershell title="PowerShell"
aws.exe configure set s3.max_concurrent_requests 20 --profile s4
aws.exe configure set s3.multipart_threshold 64MB --profile s4
aws.exe configure set s3.multipart_chunksize 64MB --profile s4
```

**macOS**

```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
```

**Linux**

```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

### 安装与配置

s5cmd 适合大量小文件。从 [s5cmd 发布页](https://github.com/peak/s5cmd/releases) 下载，或用包管理器安装：

**Windows**

```powershell
scoop install s5cmd
```

**macOS**

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

**Linux**

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

用环境变量配置：

**Windows**

```powershell title="PowerShell"
$env:AWS_ACCESS_KEY_ID="你的 AccessKey"
$env:AWS_SECRET_ACCESS_KEY="你的 SecretKey"
$env:AWS_REGION="cn-east-1"
$env:S3_ENDPOINT_URL="https://s3.bitiful.net"
```

**macOS**

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

**Linux**

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

### 常用命令

**Windows**

```powershell title="PowerShell"
# 列出
s5cmd.exe ls s3://my-bucket/images/

# 上传目录
s5cmd.exe cp 'D:\dist\*' s3://my-bucket/site/

# 下载目录
s5cmd.exe cp 's3://my-bucket/site/*' D:\dist\

# 同步
s5cmd.exe sync D:\dist\ s3://my-bucket/site/

# 批量删除
s5cmd.exe rm 's3://my-bucket/tmp/*'

# 统计容量
s5cmd.exe du --humanize 's3://my-bucket/site/*'

# 查看内容、从标准输入上传
s5cmd.exe cat s3://my-bucket/notes/todo.txt
echo "hello" | s5cmd.exe pipe s3://my-bucket/notes/hello.txt

# 临时下载链接
s5cmd.exe presign --expire 1h s3://my-bucket/images/logo.png
```

**macOS**

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

# 上传目录
s5cmd cp './dist/*' s3://my-bucket/site/

# 下载目录
s5cmd cp 's3://my-bucket/site/*' ./dist/

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

# 批量删除
s5cmd rm 's3://my-bucket/tmp/*'

# 统计容量
s5cmd du --humanize 's3://my-bucket/site/*'

# 查看内容、从标准输入上传
s5cmd cat s3://my-bucket/notes/todo.txt
echo "hello" | s5cmd pipe s3://my-bucket/notes/hello.txt

# 临时下载链接
s5cmd presign --expire 1h s3://my-bucket/images/logo.png
```

**Linux**

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

# 上传目录
s5cmd cp './dist/*' s3://my-bucket/site/

# 下载目录
s5cmd cp 's3://my-bucket/site/*' ./dist/

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

# 批量删除
s5cmd rm 's3://my-bucket/tmp/*'

# 统计容量
s5cmd du --humanize 's3://my-bucket/site/*'

# 查看内容、从标准输入上传
s5cmd cat s3://my-bucket/notes/todo.txt
echo "hello" | s5cmd pipe s3://my-bucket/notes/hello.txt

# 临时下载链接
s5cmd presign --expire 1h s3://my-bucket/images/logo.png
```

把多条命令写进文件，一次并发执行，先后顺序不固定：

文件里的本地路径用 `/` 分隔，Windows 也一样，例如 `D:/backup/a.zip`。

```text title="commands.txt"
cp ./a.zip s3://my-bucket/backup/a.zip
cp ./b.zip s3://my-bucket/backup/b.zip
rm s3://my-bucket/tmp/old.txt
```

**Windows**

```powershell title="PowerShell"
s5cmd.exe run commands.txt
```

**macOS**

```bash
s5cmd run commands.txt
```

**Linux**

```bash
s5cmd run commands.txt
```

调整并发：

**Windows**

```powershell title="PowerShell"
s5cmd.exe --numworkers 64 cp 'D:\data\*' s3://my-bucket/data/
```

**macOS**

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

**Linux**

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

默认并发是 256，可能触发 [QPS 限制](https://docs.bitiful.com/bitiful-s4/limits.md)，出现 429 时调低。

## 常见问题

<details>
<summary>aws configure 里要填 output 吗</summary>

可以不填。需要脚本处理时，在命令后加 `--output json` 或 `--output text`。

</details>

<details>
<summary>aws s3 mb 返回 403</summary>

子账户不能创建桶，请在 [控制台](https://docs.bitiful.com/bitiful-s4/handbook/buckets.md) 创建。

</details>

<details>
<summary>aws s3 ls 是空的</summary>

只会列出子账户有授权的桶，请先 [授权](https://docs.bitiful.com/bitiful-s4/handbook/permissions.md)。

</details>

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

没有设置 Endpoint，请求发到了 AWS。用 `aws configure list --profile s4` 检查，或在命令里加 `--endpoint-url`。

</details>

<details>
<summary>SignatureDoesNotMatch</summary>

检查密钥是否多了空格、Region 是否为 `cn-east-1`、本机时间是否准确。

</details>
