簡明 s3cmd 安裝與使用手冊(part one)
Preface
S3是亞馬遜AWS提供的簡單存儲服務(可以理解爲有公網域名的大容量高可用存儲)
S3配合CloudFront服務可作爲CDN使用,它提供多節點全球發佈
在使用過程中,一般是通過web頁面上傳內容到S3 Bucket(Bucket桶,S3裏存內容的容器)
這個過程緩慢繁瑣容易中斷
推薦S3cmd這個軟件用命令行實現
如下就是這個命令行工具的簡明手冊
1) 註冊 Amazon AWS / S3
首先得註冊S3服務,這個一般賬號創建完畢之後應該都有,然後創建用戶,這時會生成
你的Access Key and Secret Key,下載保存好備用
這個key對是你通過命名行工具控制AWS資源的信任驗證
當你具有AWS某項權限之後,就可以通過你的keys命令行控制了
2) 安裝s3cmd並設置好
下載下來之後,直接解壓就可以使用其中的二進制可執行文件了
你可以安裝到系統中去,這樣在任何目錄下都可以直接使用s3cmd
安裝:
python setup.py install
(注意此處的python必須是2.6版本以上)
或者使用pip來安裝,如果沒有pip命令,就
yum -y install python-pip 然後pip install s3cmd
然後設置
s3cmd --configure
執行s3cmd –configure之後會提示你輸入你的Access Key和 Secret Key
然後會提示時候進行上傳測試,測試OK之後環境設置就OK
然後保存設置
3) s3cmd mb創建新Bucket(即S3容器)
s3cmd mb s3://my-new-bucket-name s3cmd ls s3://my-new-bucket-name/*
s3cmd ls 該命令可以list出你當前的S3 bucket
4) 上傳文件到bucket:
上傳單個文件
~$ s3cmd put some-file.xml s3://public.s3tools.org/somefile.xml some-file.xml -> s3://public.s3tools.org/somefile.xml [1 of 1] 123456 of 123456 100% in 2s 51.75 kB/s done
上傳多個目錄到bucket:
~$ s3cmd put --recursive dir1 dir2 s3://public.s3tools.org/somewhere/ File 'dir1/file1-1.txt' stored as 's3://public.s3tools.org/somewhere/dir1/file1-1.txt' [1 of 5] ...
目標目錄不用提前創建,上傳時會自動創建
5) 從S3 bucket上下載文件
下載單個文件
~$ s3cmd get s3://public.s3tools.org/somefile.xml some-file-2.xml s3://public.s3tools.org/somefile.xml -> some-file-2.xml [1 of 1] 123456 of 123456 100% in 3s 35.75 kB/s done
下載整個目錄
~$ s3cmd get --recursive s3://public.s3tools.org/somewhere File s3://public.s3tools.org/somewhere/dir1/file1-1.txt saved as './somewhere/dir1/file1-1.txt' ...
注意下面兩條命名的區別
get s3://public.s3tools.org/somewhere
get s3://public.s3tools.org/somewhere/
第一條命令下載下來的東西會是/somewhere/dir1/** 即連同somewhere及其子目錄一併下載,並在本地創建相同的目錄結構
第二條下載下來的沒有somewhere目錄,即指下載該目錄的內容,在本地並不創建somewhere目錄,一般會使用recursive參數
~$ s3cmd get --recursive s3://public.s3tools.org/somewhere/*
來下載該目錄下所有內容,包括子目錄
如果你指定下載存放目錄,結果會是這樣的
~$ s3cmd get --recursive s3://public.s3tools.org/somewhere /tmp File s3://public.s3tools.org/somewhere/dir1/file1-1.txt saved as '/tmp/dir1/file1-1.txt' ...
6) 刪除操作:
刪除 s3://public.s3tools.org/somewhere/下的所有內容
~$ s3cmd del --recursive s3://public.s3tools.org/somewhere/ File s3://public.s3tools.org/somewhere/dir1/file1-1.txt deleted ...
刪除整個S3 bucket
~$ s3cmd rb s3://public.s3tools.org ERROR: S3 error: 409 (BucketNotEmpty): The bucket you tried to delete is not empty
注意刪除整個S3 bucket之前你必須確保它是空的
確保是空的bucket之後用上面的命令刪除bucket
或者強行刪除 - -force
7) 同步操作
1、同步當前目錄下所有文件
~$ s3cmd sync ./ s3://my-bucket-name/
2、加 ” - -delete-removed”參數後,會刪除本地不存在的文件。
~$ s3cmd sync --delete-removed ./ s3://my-bucket-name/
3、加 ” - -skip-existing”參數後,不進行MD5校驗,直接跳過本地已存在的文件。
~$ s3cmd sync --skip-existing ./ s3://my-bucket-name/
8) 高級同步操作
1、排除、包含規則(- -exclude 、- -include)
~$ s3cmd sync --exclude '*.doc' --include 'dir2/*' ./ s3://my-bucket-name/
2、從文件中載入排除或包含規則。(- -exclude-from、- -include-from)
~$ s3cmd sync --exclude-from exclude.txt ./ s3://my-bucket-name/
exclude.txt 文件內容:
# comments here *.jpg *.gif
3、排除或包含規則支持正則表達式
--rexclude 、--rinclude、--rexclude-from、--rinclude-from
S3cmd官網 http://s3tools.org
亞馬遜AWS S3服務官網 http://aws.amazon.com/s3