天天看點

s3cmd 安裝使用指南

https://wangyan.org/blog/s3cmd-how-to-use.html

s3cmd 安裝使用指南

s3cmd 是一款 Amazon S3 指令行工具。它不僅能上傳、下載下傳、同步,還能設定權限,下面是完整的安裝使用指南。

一、安裝方法

方法一:(Debian/Ubuntu )

1
2
3
      
wget -O- -q http://s3tools.org/repo/deb-all/stable/s3tools.key | sudo apt-key add -
wget -O/etc/apt/sources.list.d/s3tools.list http://s3tools.org/repo/deb-all/stable/s3tools.list
apt-get update && sudo apt-get install s3cmd      

方法二:

1
2
3
4
      
wget http://nchc.dl.sourceforge.net/project/s3tools/s3cmd/1.0.0/s3cmd-1.0.0.tar.gz
tar -zxf s3cmd-1.0.0.tar.gz -C /usr/local/
mv /usr/local/s3cmd-1.0.0/ /usr/local/s3cmd/
ln -s /usr/local/s3cmd/s3cmd /usr/bin/s3cmd      

二、使用方法

1、配置,主要是 Access Key ID 和 Secret Access Key

1
      
s3cmd --configure      

2、列舉所有 Buckets。(bucket 相當于根檔案夾)

1
      
s3cmd ls      

3、建立 bucket,且 bucket 名稱是唯一的,不能重複。

1
      
s3cmd mb s3://my-bucket-name      

4、删除空 bucket

1
      
s3cmd rb s3://my-bucket-name      

5、列舉 Bucket 中的内容

1
      
s3cmd ls s3://my-bucket-name      

6、上傳 file.txt 到某個 bucket,

1
      
s3cmd put file.txt s3://my-bucket-name/file.txt      

7、上傳并将權限設定為所有人可讀

1
      
s3cmd put --acl-public file.txt s3://my-bucket-name/file.txt      

8、批量上傳檔案

1
      
s3cmd put ./* s3://my-bucket-name/      

9、下載下傳檔案

1
      
s3cmd get s3://my-bucket-name/file.txt file.txt      

10、批量下載下傳

1
      
s3cmd get s3://my-bucket-name/* ./      

11、删除檔案

1
      
s3cmd del s3://my-bucket-name/file.txt      

12、來獲得對應的bucket所占用的空間大小

1
      
s3cmd du -H s3://my-bucket-name      

三、目錄處理規則

以下指令都能将dir1 中的檔案上傳至my-bucket-name,但效果隻截然不同的。

1)dir1 不帶"/"斜杠,那麼dir1會作為檔案路徑的一部分,相當于上傳整個dir1目錄,即類似 "cp -r dir1/"

1
2
      
~/demo$ s3cmd put -r dir1 s3://my-bucket-name/
dir1/file1-1.txt -> s3://my-bucket-name/dir1/file1-1.txt  [1 of 1]      

2)帶"/"斜杠的 dir1,相當于上傳dir1目錄下的所有檔案,即類似 "cp ./* "

1
2
      
~/demo$ s3cmd put -r dir1/ s3://my-bucket-name/
dir1/file1-1.txt -> s3://my-bucket-name/file1-1.txt  [1 of 1]      

四、同步方法

這是s3cmd 使用難點,但卻是最實用的功能。官方使用說明見《s3cmd sync HowTo》

首先明确,同步操作是要進行MD5校驗的,隻有當檔案不同時,才會被傳輸。

4.1、正常同步操作

1、同步目前目錄下所有檔案

1
      
s3cmd sync  ./  s3://my-bucket-name/      

2、加 "--dry-run"參數後,僅列出需要同步的項目,不實際進行同步。

1
      
s3cmd sync  --dry-run ./  s3://my-bucket-name/      

3、加 " --delete-removed"參數後,會删除本地不存在的檔案。

1
      
s3cmd sync  --delete-removed ./  s3://my-bucket-name/      

4、加 " --skip-existing"參數後,不進行MD5校驗,直接跳過本地已存在的檔案。

1
      
s3cmd sync  --skip-existing ./  s3://my-bucket-name/      

4.2、進階同步操作

4.2.1、排除、包含規則(--exclude 、--include)

file1-1.txt被排除,file2-2.txt同樣是txt格式卻能被包含。

1
2
3
      
~/demo$ s3cmd sync --dry-run --exclude '*.txt' --include 'dir2/*' ./  s3://my-bucket-name/
exclude: dir1/file1-1.txt
upload: ./dir2/file2-2.txt -> s3://my-bucket-name/dir2/file2-2.txt      

4.2.2、從檔案中載入排除或包含規則。(--exclude-from、--include-from)

1
      
s3cmd sync  --exclude-from pictures.exclude ./  s3://my-bucket-name/      

pictures.exclude 檔案内容

1
2
3
      
# Hey, comments are allowed here ;-)
*.jpg
*.gif      

4.2.3、排除或包含規則支援正規表達式

1
      
--rexclude 、--rinclude、--rexclude-from、--rinclude-from      

1
2
3
      
wget -O- -q http://s3tools.org/repo/deb-all/stable/s3tools.key | sudo apt-key add -
wget -O/etc/apt/sources.list.d/s3tools.list http://s3tools.org/repo/deb-all/stable/s3tools.list
apt-get update && sudo apt-get install s3cmd      
1
2
3
4
      
wget http://nchc.dl.sourceforge.net/project/s3tools/s3cmd/1.0.0/s3cmd-1.0.0.tar.gz
tar -zxf s3cmd-1.0.0.tar.gz -C /usr/local/
mv /usr/local/s3cmd-1.0.0/ /usr/local/s3cmd/
ln -s /usr/local/s3cmd/s3cmd /usr/bin/s3cmd      

1
      
s3cmd --configure      
1
      
s3cmd ls      
1
      
s3cmd mb s3://my-bucket-name      
1
      
s3cmd rb s3://my-bucket-name      
1
      
s3cmd ls s3://my-bucket-name      
1
      
s3cmd put file.txt s3://my-bucket-name/file.txt      
1
      
s3cmd put --acl-public file.txt s3://my-bucket-name/file.txt      
1
      
s3cmd put ./* s3://my-bucket-name/      
1
      
s3cmd get s3://my-bucket-name/file.txt file.txt      
1
      
s3cmd get s3://my-bucket-name/* ./      
1
      
s3cmd del s3://my-bucket-name/file.txt      
1
      
s3cmd du -H s3://my-bucket-name      

1
2
      
~/demo$ s3cmd put -r dir1 s3://my-bucket-name/
dir1/file1-1.txt -> s3://my-bucket-name/dir1/file1-1.txt  [1 of 1]      
1
2
      
~/demo$ s3cmd put -r dir1/ s3://my-bucket-name/
dir1/file1-1.txt -> s3://my-bucket-name/file1-1.txt  [1 of 1]      

1
      
s3cmd sync  ./  s3://my-bucket-name/      
1
      
s3cmd sync  --dry-run ./  s3://my-bucket-name/      
1
      
s3cmd sync  --delete-removed ./  s3://my-bucket-name/      
1
      
s3cmd sync  --skip-existing ./  s3://my-bucket-name/      

1
2
3
      
~/demo$ s3cmd sync --dry-run --exclude '*.txt' --include 'dir2/*' ./  s3://my-bucket-name/
exclude: dir1/file1-1.txt
upload: ./dir2/file2-2.txt -> s3://my-bucket-name/dir2/file2-2.txt      
1
      
s3cmd sync  --exclude-from pictures.exclude ./  s3://my-bucket-name/      
1
2
3
      
# Hey, comments are allowed here ;-)
*.jpg
*.gif      
1
      
--rexclude 、--rinclude、--rexclude-from、--rinclude-from