s3cmd s3命令行工具


Amazon S3 Tools: Command Line S3 Client Software and S3 Backup

官方網站🌐

開發語言 python

安裝配置參考📙

安裝

pip install s3cmd
# 或
yum install python-s3cmd

推薦版本 2+,2.0.0 版本之前測試過有bug,附錄部分有說明。

配置

  1. 通過命令行傳遞參數配置

    s3cmd --configure \
            --access_key=<access_key> \
            --secret_key=<secret_key> \
            --region=<region> \
            --host=<endpoint> \
            --host-bucket=<endpoint> \
            --no-ssl
    
  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
    

如果使用的是aws s3,配置可以精簡為只有access_key和secret_key

[default]
access_key = <access_key>
secret_key = <secret_key>

用法

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、上傳
s3cmd put file.txt s3://my-bucket-name/file.txt

支持批量上傳,直接指定多個文件,如
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

上傳大文件時,使用 --multipart-chunk-size-mb=size 指定的分片大小必須是4的倍數,否則上傳會報 400(InvalidPartOrder)

其他常用參數

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

🌴 附錄

  1. 2.0.0版本上傳大文件時有bug


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM