使用 AWS CLI 与 s5cmd
AWS CLI 是官方命令行工具,功能最全;s5cmd 用高并发处理大量文件,速度更快。两者都可以直接连接 S4。
前置条件
- 已 创建存储桶;
- 已 创建子账户与 AccessKey,并完成 授权。
AWS CLI
安装与配置
按 官方说明 安装,然后为 S4 单独建一个 profile:
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 s4endpoint_url 写进 profile 需要 AWS CLI 2.13 以上。更旧的版本,每条命令都要加 --endpoint-url https://s3.bitiful.net。
之后的命令都加上 --profile s4,或先设置环境变量:
export AWS_PROFILE=s4上传与下载
# 上传
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 s3://my-bucket/images/logo.png ./logo.png同步
# 只上传有变化的文件
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查看与删除
aws s3 ls # 有授权的桶
aws s3 ls s3://my-bucket/images/ # 一级目录
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 # 删除整个目录,无法恢复预签名下载链接
aws s3 presign s3://my-bucket/images/logo.png --expires-in 3600有效期单位是秒,最长 604800(7 天)。
移动与条件写
# 原生移动,不复制数据
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 是先复制再删除,大文件请用上面的移动。--if-none-match 需要较新的 AWS CLI。
调整并发与分片
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 s4s5cmd
安装与配置
从 s5cmd 发布页 下载,或用 Homebrew 安装:
brew install peak/tap/s5cmd用环境变量配置:
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常用命令
# 列出
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 --numworkers 64 cp './data/*' s3://my-bucket/data/默认并发是 256,可能触发 QPS 限制,出现 429 时调低。