已移除 的 Python 2.7、3.4 和 AWS CLI 第 1 版3.5。如需詳細資訊,請查看 的AWS CLI 第 1 版一節關於 AWS CLI 版本。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭配 使用高階 (s3) 命令 AWS CLI
本主題描述如何使用 中的Amazon S3aws s3
命令管理 儲存貯體和物件AWS CLI。
高階 aws s3
命令可簡化Amazon S3物件的管理程序。這些命令可讓您管理 Amazon S3 本身和本機 的命令。
當您使用 aws s3
命令將大型物件上傳至 Amazon S3 儲存貯體時, AWS CLI 會自動執行分段上傳。使用這些aws s3
命令時,您無法繼續失敗的上傳作業。
如果分段上傳因為逾時而失敗,或是您在 中手動取消AWS CLI, AWS CLI 會停止上傳,並清除任何已建立的檔案。此程序需要幾分鐘的時間。
如果 kill 命令或系統失敗而取消了分段上傳或清除程序,所建立的檔案會保留在 Amazon S3 儲存貯體中。若要清除分段上傳,請使用 s3api abort-multipart-upload 命令。
如需詳細資訊,請查看中的分段上傳概觀Amazon Simple Storage Service 開發人員指南。
Prerequisites
若要執行命令s3
,您需要:
-
安裝及設定 AWS CLI. 如需詳細資訊,請查看 安裝、更新和解除安裝AWS CLI 和 組態基礎概念。
-
了解這些Amazon S3術語:
-
儲存貯體 – 最上層Amazon S3資料夾。
-
前綴 – 儲存貯體中的 Amazon S3 資料夾。
-
物件 裝載在 – 儲存貯體中Amazon S3的任何項目。
-
建立儲存貯體
使用 s3 mb
命令來製作儲存貯體。儲存貯體名稱必須是全域唯一 (全部 都是唯一Amazon S3),且應符合 DNS 標準。
儲存貯體名稱可包含小寫字母、數字、連字號和句號。儲存貯體名稱的開頭和結尾只能使用字母或數字,連字號或句號旁邊不能使用句號。
語法
$
aws s3 mb <target> [--options]
以下範例會建立s3://bucket-name
儲存貯體。
$
aws s3 mb s3://
bucket-name
列出儲存貯體與物件
若要列出您的儲存貯體、資料夾或物件,請使用 s3 ls
命令。使用不帶目標或選項的 命令,可列出所有儲存貯體。
語法
$
aws s3 ls <target> [--options]
如需使用此命令的幾個常用選項以及範例,請查看s3 命令的常用選項。如需可用選項的完整清單,請查看 s3 ls
中的 AWS CLI Command Reference。
下列範例列出所有 Amazon S3 儲存貯體。
$
aws s3 ls
2018-12-11 17:08:50 my-bucket 2018-12-14 14:55:44 my-bucket2
下列命令列出儲存貯體中的所有物件和字首。在此範例輸出中, 字首example/
具有一個名為 的檔案MyFile1.txt
。
$
aws s3 ls
s3://bucket-name
PRE example/ 2018-12-04 19:05:48 3 MyFile1.txt
您可以將特定的字首包含在命令中,將輸出篩選為該字首。下列命令列出 中的物件 bucket-name/example/
(也就是 中的物件 bucket-name
依字首篩選 example/
).
$
aws s3 ls
s3://bucket-name/example/
2018-12-06 18:59:32 3 MyFile1.txt
Delete 儲存貯體
若要删除儲存貯體,請使用 s3
rb
命令。
語法
$
aws s3 rb <target> [--options]
下列範例會移除s3://bucket-name
儲存貯體。
$
aws s3 rb
s3://bucket-name
根據預設,儲存貯體必須為空才能成功操作。若要移除非空白的儲存貯體,您需要包含 --force
選項。如果您使用的版本控制儲存貯體包含先前移除—但已保留—的物件,則此命令不允許您移除儲存貯體。您必須先刪除所有內容。
下列範例會删除儲存貯體中的所有物件和字首,然後删除儲存貯體。
$
aws s3 rb
s3://bucket-name
--force
Delete 物件
若要删除儲存貯體中的物件或您的本機資料夾,請使用 s3 rm
命令。
語法
$
aws s3 rm <target> [--options]
如需與此命令搭配使用的幾個常用選項以及範例,請查看 s3 命令的常用選項。如需完整的選項清單,請前往 s3 rm
中的 AWS CLI Command Reference。
下列範例filename.txt
從 删除 s3://bucket-name/example
。
$
aws s3 rm s3://bucket-name/example/filename.txt --recursive
下列範例s3://bucket-name/example
使用 --recursive
選項,從 中移除所有物件。
$
aws s3 rm s3://bucket-name/example --recursive
移動物件
使用 s3 mv
命令從儲存貯體或本機資料夾移動物件。
語法
$
aws s3 mv <source> <target> [--options]
如需使用此命令的幾個常用選項以及範例,請查看 s3 命令的常用選項。如需可用選項的完整清單,請查看 s3 mv
中的 AWS CLI Command Reference。
以下範例會將所有物件從 移至 s3://bucket-name/example
s3://my-bucket/
。
$
aws s3 mv s3://bucket-name/example s3://my-bucket/
下列範例使用 Amazon S3 命令,將本機檔案從目前的工作資料夾移至 s3 cp
儲存貯體。
$
aws s3 mv filename.txt s3://bucket-name
以下範例會將 檔案從 Amazon S3 儲存貯體移至您目前的工作資料夾,其中 ./
指定您目前的工作資料夾。
$
aws s3 mv s3://bucket-name/filename.txt ./
複製物件
使用 s3 cp
命令從儲存貯體或本機資料夾複製物件。
語法
$
aws s3 cp <source> <target> [--options]
您可以使用破折號參數來串流檔案至標準輸入 (stdin
) 或標準輸出 (stdout
)。
如果您使用的是 PowerShell,Shell 可能會改變 CRLF 的編碼,或將 CRLF 新增至管道輸入或輸出,或是重新導向輸出。
命令s3 cp
使用下列語法,從 將檔案串流上傳至stdin
指定的 儲存貯體。
語法
$
aws s3 cp - <target> [--options]
命令s3 cp
使用下列語法來為 下載 Amazon S3 檔案串流stdout
。
語法
$
aws s3 cp <target> [--options] -
如需使用此命令的幾個常見選項以及範例,請查看 s3 命令的常用選項。如需完整的選項清單,請前往 s3 cp
中的 AWS CLI Command Reference。
下列範例會將 中的所有物件複製到 s3://bucket-name/example
s3://my-bucket/
。
$
aws s3 cp s3://bucket-name/example s3://my-bucket/
下列範例會使用 Amazon S3 命令,將本機檔案從您目前的工作資料夾複製到 s3 cp
儲存貯體。
$
aws s3 cp filename.txt s3://bucket-name
以下範例會從您的 Amazon S3 儲存貯體複製檔案到您目前的工作目標,其中 ./
指定您目前的工作目標。
$
aws s3 cp s3://bucket-name/filename.txt ./
以下範例使用 cat 文字編輯器將文字「hello world」串流至 s3://bucket-name/filename.txt
檔案。
$
cat "hello world" | aws s3 cp - s3://bucket-name/filename.txt
下列範例s3://bucket-name/filename.txt
將檔案串流到 stdout
,並將這些物件列印到主控台。
$
aws s3 cp s3://bucket-name/filename.txt -
hello world
下列範例會將 的 資訊串流s3://bucket-name/pre
至 stdout
,使用 bzip2
命令壓縮檔案,並將名為 的新壓縮檔案上傳至 key.bz2
s3://bucket-name
。
$
aws s3 cp s3://bucket-name/pre - | bzip2 --best | aws s3 cp - s3://bucket-name/key.bz2
同步物件
s3 sync
命令會同步儲存貯體和目標,或是兩個儲存貯體的 。一般而言, 會在來源和目標之間s3 sync
複製缺少或過時的檔案或物件。但是您也可以提供 --delete
選項,來從目標中移除沒有出現在來源中的檔案或物件。
語法
$
aws s3 sync <source> <target> [--options]
如需使用此命令的幾個常見選項以及範例,請查看 s3 命令的常用選項。如需完整的選項清單,請前往 s3 sync
中的 AWS CLI Command Reference。
下列範例會同步名為 Amazon S3my-bucket 之儲存貯體中 字首 (名為 路徑) 的 ,以及目前的工作目標。
s3 sync
會更新任何大小或修改時間與目的地中名稱相同的檔案不同的檔案。輸出顯示在同步期間所執行的特定操作。請注意,操作會遞迴同步子指導MySubdirectory
及其與 的組合s3://my-bucket/path/MySubdirectory
。
$
aws s3 sync . s3://my-bucket/path
upload: MySubdirectory\MyFile3.txt to s3://my-bucket/path/MySubdirectory/MyFile3.txt upload: MyFile2.txt to s3://my-bucket/path/MyFile2.txt upload: MyFile1.txt to s3://my-bucket/path/MyFile1.txt
以下範例延伸前一個範例,示範如何使用 --delete
選項。
// Delete local file
$
rm ./MyFile1.txt
// Attempt sync without --delete option - nothing happens
$
aws s3 sync . s3://my-bucket/path
// Sync with deletion - object is deleted from bucket
$
aws s3 sync . s3://my-bucket/path --delete
delete: s3://my-bucket/path/MyFile1.txt // Delete object from bucket
$
aws s3 rm s3://my-bucket/path/MySubdirectory/MyFile3.txt
delete: s3://my-bucket/path/MySubdirectory/MyFile3.txt // Sync with deletion - local file is deleted
$
aws s3 sync s3://my-bucket/path . --delete
delete: MySubdirectory\MyFile3.txt // Sync with Infrequent Access storage class
$
aws s3 sync . s3://my-bucket/path --storage-class STANDARD_IA
使用 --delete
選項時, --exclude
和 --include
選項可以篩選要在s3
sync
操作期間删除的檔案或物件。在此情況下,參數字串必須針對目標目錄或儲存貯體,指定檔案要排除或包含在刪除操作中。如下列範例所示。
Assume local directory and s3://my-bucket/path currently in sync and each contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt '''
// Sync with delete, excluding files that match a pattern. MyFile88.txt is deleted, while remote MyFile1.txt is not.
$
aws s3 sync . s3://my-bucket/path --delete --exclude "path/MyFile?.txt"
delete: s3://my-bucket/path/MyFile88.txt '''
// Sync with delete, excluding MyFile2.rtf - local file is NOT deleted
$
aws s3 sync s3://my-bucket/path . --delete --exclude "./MyFile2.rtf"
download: s3://my-bucket/path/MyFile1.txt to MyFile1.txt ''' // Sync with delete, local copy of MyFile2.rtf is deleted
$
aws s3 sync s3://my-bucket/path . --delete
delete: MyFile2.rtf
s3 命令的常用選項
下列選項經常用於本主題中所述的命令。如需可用於 命令的完整選項清單,請查看 中的特定命令AWS CLI Command Reference。
- acl
-
s3 sync
和s3 cp
可以使用--acl
選項。這可讓您設定複製到 之檔案的存取許可Amazon S3。--acl
選項接受private
、 和public-read
public-read-write
值。如需詳細資訊,請查看 中的 標準 Amazon Simple Storage Service 開發人員指南ACL。$
aws s3 sync . s3://my-bucket/path --acl public-read
- 排除
-
當您使用
s3 cp
、s3 mv
s3 sync
、 或s3 rm
命令時,您可以使用--exclude
或--include
選項來篩選結果。--exclude
選項會設定規則,使其排除來自 命令的物件,且選項會依照指定的順序套用。如以下範例所示。Local directory contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt
// Exclude all .txt files, resulting in only MyFile2.rtf being copied
$
aws s3 cp . s3://my-bucket/path --exclude "*.txt"
// Exclude all .txt files but include all files with the "MyFile*.txt" format, resulting in, MyFile1.txt, MyFile2.rtf, MyFile88.txt being copied
$
aws s3 cp . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt"
// Exclude all .txt files, but include all files with the "MyFile*.txt" format, but exclude all files with the "MyFile?.txt" format resulting in, MyFile2.rtf and MyFile88.txt being copied
$
aws s3 cp . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt" --exclude "MyFile?.txt"
- 包含
-
當您使用
s3 cp
、s3 mv
s3 sync
、 或s3 rm
命令時,您可以使用--exclude
或--include
選項來篩選結果。--include
選項會設定規則,使其僅包含為命令指定的物件,且選項會依照指定的順序套用。如以下範例所示。Local directory contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt
// Include all .txt files, resulting in MyFile1.txt and MyFile88.txt being copied
$
aws s3 cp . s3://my-bucket/path --include "*.txt"
// Include all .txt files but exclude all files with the "MyFile*.txt" format, resulting in no files being copied
$
aws s3 cp . s3://my-bucket/path --include "*.txt" --exclude "MyFile*.txt"
// Include all .txt files, but exclude all files with the "MyFile*.txt" format, but include all files with the "MyFile?.txt" format resulting in MyFile1.txt being copied
$
aws s3 cp . s3://my-bucket/path --include "*.txt" --exclude "MyFile*.txt" --include "MyFile?.txt"
- 授予
-
s3 cp
s3 mv
和s3 sync
命令包含--grants
選項,您可用來向指定使用者或群組授予對物件的許可。使用下列語法,針對許可清單設定--grants
選項。以您自己的值取代Permission
Grantee_Type
、 和Grantee_ID
。語法
--grants
Permission
=Grantee_Type
=Grantee_ID
[Permission
=Grantee_Type
=Grantee_ID
...]每個數值包含下列元素:
-
Permission
– 指定授予的許可。可以設定為read
、readacl
、writeacl
或full
。 -
Grantee_Type
– 指定如何辨識被授予者。可以設定為uri
、emailaddress
或id
。 -
Grantee_ID
– 根據 來指定承授者Grantee_Type
.-
uri
– 群組的 URI。如需詳細資訊,請查看誰是承授者? -
emailaddress
– 帳戶的電子郵件地址。 -
id
– 帳戶的正式 ID。
-
如需Amazon S3存取控制的詳細資訊,請前往存取控制。
下列範例將物件複製到儲存貯體中。它會授予物件
read
的許可給每個人,以及與 相關聯的 帳號full
許可 (read
readacl
、 和writeacl
user@example.com
)。$
aws s3 cp file.txt s3://my-bucket/ --grants
read=uri=http://acs.amazonaws.com/groups/global/AllUsers full=emailaddress=user@example.com
您也可以為您上傳到
REDUCED_REDUNDANCY
的物件,指定非預設儲存等級 (STANDARD_IA
或 Amazon S3. 若要這麼做,請使用--storage-class
選項。$
aws s3 cp file.txt s3://my-bucket/
--storage-class REDUCED_REDUNDANCY
-
- recursive
-
使用此選項時,會在指定之資料夾或字首下的所有檔案或物件上執行 命令。下列範例會删除
s3://my-bucket/path
和其所有資訊。$
aws s3 rm s3://my-bucket/path --recursive
References
AWS CLI 參考:
服務參考:
-
中的使用 Amazon S3 儲存貯體 Amazon Simple Storage Service 開發人員指南
-
在 中使用Amazon S3物件 Amazon Simple Storage Service 開發人員指南
-
在 中使用字首與分隔符號以階層方式列出金鑰 Amazon Simple Storage Service 開發人員指南
-
使用 中的 適用於 .NET 的 AWS 開發套件 (低階) 中止分段上傳至 S3 儲存貯體 Amazon Simple Storage Service 開發人員指南