1. s3cmd sync命令常用選項說明
- --list-md5:結合list一起使用,打印md5
- -H, --human-readable-sizes:人性化文件大小
- -v, --verbose:顯示詳細的輸出
- -d, --debug:調試信息
- --limit-rate=LIMITRATE:將上傳或下載速度限制為每秒字節數。 數量可以用字節、k 后綴的千字節或 m 后綴的兆字節表示。
- -n, --dry-run:只顯示應該上傳或下載的內容,但實際上並不這樣做。 仍可能執行 S3 請求以獲取存儲桶列表和其他信息(僅適用於文件傳輸命令)
- --skip-existing:跳過存在於目的地的文件(僅適用於 [get] 和 [sync] 命令)。請謹慎使用此選項!
- --delete-removed:刪除沒有對應源文件的目標對象 [sync]。請謹慎使用此選項!
- -p, --preserve:保留文件系統屬性(模式、所有權、時間戳)。 [sync] 命令的默認值。
- -q, --quiet:不輸出信息到標准輸出。
文件同步相關:
- --exclude=GLOB:通配
- --exclude-from=FILE:從文件讀取排除列表
- --rexclude=REGEXP:正則形式的匹配排除
- --rexclude-from=FILE:從文件讀取正則形式的匹配排除
- --include=GLOB:通配
- --include-from=FILE:從文件讀取文件列表
- --rinclude=REGEXP:正則匹配
- --rinclude-from=FILE:從文件讀取正則匹配
ACL:
$ 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
2. 安裝s3cmd
$ pip install s3cmd
# 或:
$ yum install python-s3cmd
# 或:
$ yum install s3cmd
推薦版本2.0.1+,2.0.0版本上傳大文件時有Bug。
3. 配置s3cmd
執行 s3cmd --configure 生成配置文件,一路Enter,注意跳過認證並保存配置。
......
...
Test access with supplied credentials? [Y/n] n
Save settings? [y/N] y
Configuration saved to '/root/.s3cfg'
修改一下幾項:
$ vim /root/.s3cfg
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
- 其中,access_key和secret_key是在本地創建S3用戶時獲得。
- host_base是S3服務所使用的ip地址(包括端口號)。
- host_bucket為S3用戶下的一個bucket(可在配置之后再創建,但該字段不能為空)。
s3cmd 2.2.0(最新版),在某些不兼容v4的情況下,需要使用signature_v2,否則會發起兩次簽名重試請求。
4. 例子
One of the most powerful commands of s3cmd is s3cmd sync used for synchronising complete directory trees to or from
remote S3 storage. To some extent s3cmd put and s3cmd get share a similar behaviour with sync.
Basic usage common in backup scenarios is as simple as:
s3cmd sync /local/path/ s3://test-bucket/backup/
This command will find all files under /local/path directory and copy them to corresponding paths under
s3://test-bucket/backup on the remote side. For example:
/local/path/file1.ext -> s3://bucket/backup/file1.ext
/local/path/dir123/file2.bin -> s3://bucket/backup/dir123/file2.bin
However if the local path doesn't end with a slash the last directory's name is used on the remote side as well.
Compare these with the previous example:
s3cmd sync /local/path s3://test-bucket/backup/
will sync:
/local/path/file1.ext -> s3://bucket/backup/path/file1.ext
/local/path/dir123/file2.bin -> s3://bucket/backup/path/dir123/file2.bin
To retrieve the files back from S3 use inverted syntax:
s3cmd sync s3://test-bucket/backup/ ~/restore/
that will download files:
s3://bucket/backup/file1.ext -> ~/restore/file1.ext
s3://bucket/backup/dir123/file2.bin -> ~/restore/dir123/file2.bin
Without the trailing slash on source the behaviour is similar to what has been demonstrated with upload:
s3cmd sync s3://test-bucket/backup ~/restore/
will download the files as:
s3://bucket/backup/file1.ext -> ~/restore/backup/file1.ext
s3://bucket/backup/dir123/file2.bin -> ~/restore/backup/dir123/file2.bin
5. 使用注意事項
- 上傳大文件時,使用 --multipart-chunk-size-mb=size 指定的分片大小必須是4的倍數,否則上傳會報 400(InvalidPartOrder)。