s3cmd 研究

最近有一個需求要做到同步把 S3 檔案抓下來,去找了一點資料找到一個工具可以做到這件事情,以下是研究結果跟踩到的問題,跟大家分享

基礎設定

因為需要抓 S3 檔案,所以需要 AWS API Key,這個部分可以在下指令的時候帶入或是使用設定檔,這邊已設定檔為範例:

首先建立一個檔案 ~/.s3cfg 如下:

# Setup endpoint
bucket_location = us-east-1
use_https = True

# Setup access keys
access_key = Q3AM3UQ867SPQQA43P2F
secret_key = zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG

# Enable S3 v4 signature APIs
signature_v2 = False

設定完之後後面的指令就可以正常使用了!

使用 –dry-run 測試指令

每次下載檔案需要用到流量,所以如果只是要測試指令的話可以使用 --dry-run 就不會每次都下載檔案了,在前期 Debug 的時候很好用

使用 –exclude 排除檔案

通常在 S3 上面的檔案非常多,有時候並不想要全部都下載下來,這時候就可以使用 –exclude,設定規則排除檔案,如下範例排除所有 jpg 副檔名的檔案

s3cmd sync --exclude '*.jpg' --skip-existing s3://clarence/ clarence/

使用 –include 加入檔案

加入 exclude 後可能也想要把某些特別的檔案加入,就可以再用 include 把檔案加入

s3cmd sync --exclude 'folder1/*' --include 'folder1/*.mp4' --skip-existing s3://clarence/ clarence/

如果想要加入資料夾也是可以的

s3cmd sync --exclude '*' --include 'folder1/*' --include '*.mp4' --skip-existing s3://clarence/ clarence/

使用 –skip-existing

在使用 skip-existing 的時候遇到了一個問題,怎樣都不會動,不能用的指令如下:

s3cmd sync --skip-existing s3://clarence clarence

上面指令是錯的請注意!

後來去找了一下 issue 發現是指令錯誤,修正後的指令如下

s3cmd sync --exclude '*' --include 'folder1/*' --include '*.mp4' --skip-existing s3://clarence/ clarence/

參考資料

https://s3tools.org/usage