安装
pip install s3cmd # 方法一
yum install python-s3cmd # 方法二
配置
1. 通过命令行传递参数配置
s3cmd --configure \
--access_key=<access_key> \
--secret_key=<secret_key> \
--region=<region> \
--host=<endpoint> \
--host-bucket=<endpoint> \
--no-ssl
--signature-v2
2. 通过配置文件配置
~/.s3cfg
[default]
access_key = <access_key>
secret_key = <secret_key>
bucket_location = <region>
host_base = <endpoint>
host_bucket = <endpoint>
use_https = False
human_readable_sizes = True
website_index = index.html
signature_v2 = True
用法
# 1、配置,主要是 Access Key ID 和 Secret Access Key
s3cmd --configure
# 2、列举所有 Buckets。(bucket 相当于根文件夹)
s3cmd ls
# 3、创建 bucket,且 bucket 名称是唯一的,不能重复,默认创建的 bucket 是公开的。
s3cmd mb s3://my-bucket-name
# 4、删除空 bucket
s3cmd rb s3://my-bucket-name
# 5、列举 Bucket 中的内容
s3cmd ls s3://my-bucket-name
# 6、上传
# 6.1 单个上传
s3cmd put file.txt s3://my-bucket-name/file.txt
# 6.2 支持批量上传,直接指定多个文件,如
s3cmd put t.py s3://tccpoc/t.py up.py s3://tccpoc/up.py
# 如果上传终断,比如ctrl+c,会显示upload-id,按照指示,带上`--upload-id`就可以实现断点上传
# 7、上传并将权限设置为所有人可读
s3cmd put --acl-public file.txt s3://my-bucket-name/file.txt
--acl-private,# 也可以是私有
# 8、批量上传文件
s3cmd put ./* s3://my-bucket-name/
# 9、下载文件
s3cmd get s3://my-bucket-name/file.txt file.txt
# 支持批量下载,直接指定多个文件,如
s3cmd get s3://tccpoc/t.py s3://tccpoc/up.py
# 如果下载终断,比如ctrl+c,带上参数`--continue`,可以实现断点下载
# 10、批量下载
s3cmd get s3://my-bucket-name/* ./
# 11、删除文件,
s3cmd del s3://my-bucket-name/file.txt
# 支持批量删除,直接指定多个 bucket 对象,如
s3cmd del s3://my-bucket-name/file.txt s3://my-bucket-name/file2.txt
# 12、来获得对应的bucket所占用的空间大小
s3cmd du -H s3://my-bucket-name
其他常用参数
--list-md5 结合list一起使用,打印md5
-H, --human-readable-sizes 人性化文件大小
-v, --verbose 显示详细的输出
-d, --debug 调试信息
--limit-rate=LIMITRATE 限速
文件同步相关
--exclude=GLOB 通配
--exclude-from=FILE 从文件读取排除列表
--rexclude=REGEXP 正则形式的匹配排除
--rexclude-from=FILE 从文件读取正则形式的匹配排除
--include=GLOB 通配
--include-from=FILE 从文件读取文件列表
--rinclude=REGEXP 正则匹配
--rinclude-from=FILE 从文件读取正则匹配
# 示例
# s3cmd sync --exclude '*' --include 'link*' images/ s3://files
upload: 'images/link.png' -> 's3://files/link.png' [1 of 1]
8094 of 8094 100% in 0s 206.34 kB/s done
# s3cmd sync --exclude '*' --include 'link*' s3://files images2
download: 's3://files/link.png' -> 'images2/link.png' [1 of 1]
8094 of 8094 100% in 0s 323.87 kB/s done
Done. Downloaded 8094 bytes in 1.0 seconds, 7.90 kB/s.
ACL
# s3cmd set-acl s3://files/link.png --acl-public
# s3cmd modify s3://files/link.png --acl-private
# s3cmd modify s3://files/link.png --acl-public
生命周期
# 设置文件1天后过期
# s3cmd modify s3://testabc/sqr.py --add-header x-delete-after:1