

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 AWS CLI 中使用 Amazon S3
<a name="cli-services-s3"></a>


| Amazon Simple Storage Service (Amazon S3) 簡介 | 
| --- | 
|    | 

您可以使用 AWS Command Line Interface (AWS CLI) 存取 Amazon Simple Storage Service (Amazon S3) 的功能。Amazon S3 是一項高度可擴展且持久的物件儲存服務。Amazon S3 旨在提供幾乎無限制的儲存容量，使其成為滿足各種資料儲存和管理需求的理想解決方案。

Amazon S3 可讓您以物件形式存放和擷取任意數量的資料，從小型檔案到大型資料集。每個物件都存放在稱為儲存貯體的容器中，可透過 AWS 管理主控台 或以程式設計方式透過 AWS SDK、工具和 AWS CLI 加以存取和管理。

Amazon S3 包含基本儲存空間，也提供各種功能，包括生命週期管理、版本控制、可擴展性和安全性。這些功能與其他 AWS 服務 整合，讓您能夠建置雲端型解決方案來擴展您的需求。

AWS CLI 為存取 Amazon S3 提供了兩個層級的命令：
+ **s3** – 針對 AWS CLI 特別製作的自訂高階命令，可簡化執行常見任務，例如建立、操作、刪除和同步物件與儲存貯體。
+ **s3api**— 公開對所有 Amazon S3 API 操作的直接存取，使您能夠執行進階操作。

**Topics**
+ [在 中使用高階 (s3) 命令 AWS CLI](cli-services-s3-commands.md)
+ [在 中使用 API 層級 (s3api) 命令 AWS CLI](cli-services-s3-apicommands.md)
+ [中的 Amazon S3 儲存貯體生命週期指令碼範例 AWS CLI](cli-services-s3-lifecycle-example.md)

# 在 中使用高階 (s3) 命令 AWS CLI
<a name="cli-services-s3-commands"></a>

本主題說明如何使用 AWS CLI中的 [https://docs.aws.amazon.com/cli/latest/reference/s3/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html) 命令來管理 Amazon S3 儲存貯體和物件。有關本主題中未涉及的命令和其他命令範例，請參閱 *AWS CLI 參考*中的 [https://docs.aws.amazon.com/cli/latest/reference/s3/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html) 命令。

高階 `aws s3` 命令可簡化 Amazon S3 物件的管理作業。這些命令可讓您管理 Amazon S3 內部的內容以及本機目錄的內容。

**Topics**
+ [先決條件](#using-s3-commands-prereqs)
+ [開始之前](#using-s3-commands-before)
+ [建立 儲存貯體](#using-s3-commands-managing-buckets-creating)
+ [列出儲存貯體和物件](#using-s3-commands-listing-buckets)
+ [刪除儲存貯體](#using-s3-commands-delete-buckets)
+ [刪除物件](#using-s3-commands-delete-objects)
+ [移動物件](#using-s3-commands-managing-objects-move)
+ [複製物件](#using-s3-commands-managing-objects-copy)
+ [同步物件](#using-s3-commands-managing-objects-sync)
+ [s3 命令的常用選項](#using-s3-commands-managing-objects-param)
+ [Resources](#using-s3-commands-managing-buckets-references)

## 先決條件
<a name="using-s3-commands-prereqs"></a>

若要執行 `s3` 命令，您需要：
+ 安裝及設定 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)及[的身分驗證和存取憑證 AWS CLI](cli-chap-authentication.md)。
+ 您使用的設定檔必須具有允許範例執行 AWS 操作的許可。
+ 了解這些 Amazon S3 術語：
  + **儲存貯體** – 頂層 Amazon S3 資料夾。
  + **前綴** – 儲存貯體中的 Amazon S3 資料夾。
  + **物件** – 託管於 Amazon S3 儲存貯體中的任一個項目。

## 開始之前
<a name="using-s3-commands-before"></a>

本節說明在使用 `aws s3` 命令之前應注意的幾個事項。

### 大型物件上傳
<a name="using-s3-commands-before-large"></a>

當您使用 `aws s3` 命令將大型物件上傳至 Amazon S3 儲存貯體時， AWS CLI 會自動執行分段上傳。使用這些 `aws s3` 命令時，您無法恢復失敗的上傳。

如果分段上傳因逾時而失敗，或如果您在 中手動取消 AWS CLI，則 會 AWS CLI 停止上傳並清除任何已建立的檔案。此程序需要幾分鐘的時間。

如果分段上傳或清理程序因為 Kill 命令或系統故障而取消，則建立的檔案會保留在 Amazon S3 儲存貯體中。若要清理分段上傳，請使用 [s3api abort-multipart-upload](https://docs.aws.amazon.com/cli/latest/reference/s3api/abort-multipart-upload.html) 命令。

### 分段副本中的檔案屬性和標籤
<a name="using-s3-commands-before-tags"></a>

當您使用 `aws s3` 命名空間中的 AWS CLI 第 1 版命令，將檔案從一個 Amazon S3 儲存貯體位置複製到另一個 Amazon S3 儲存貯體位置，且該操作使用[分段複製](https://docs.aws.amazon.com/AmazonS3/latest/userguide/CopyingObjctsMPUapi.html)時，不會將來源物件中的檔案屬性複製到目的地物件。

根據預設，執行分段複製的`s3`命名空間中的第 2 `content-disposition` AWS CLI 版命令會將所有標籤和下列屬性集從來源傳輸到目的地複本：`content-type`、`content-language`、`content-encoding`、`cache-control`、`expires`、 和 `metadata`。

如果您使用第 1 AWS CLI 版，這可能會導致對 Amazon S3 端點進行額外的 AWS API 呼叫。這些可能包括：`HeadObject`、`GetObjectTagging`、和 `PutObjectTagging`。

如果您需要變更第 2 AWS CLI 版命令中的此預設行為，請使用 `--copy-props` 參數指定下列其中一個選項：
+ **預設** – 預設值。指定副本包括連接至來源物件的所有標籤，以及用於非多段副本的 `--metadata-directive` 參數所包含的屬性：`content-type`、`content-language`、`content-encoding`、`content-disposition`、`cache-control`、`expires` 和 `metadata`。
+ **metadata-directive** – 指定副本只包含用於非分段副本的 `--metadata-directive` 參數所包含的屬性。它不會複製任何標籤。
+ **none** – 指定副本不包含來源物件的任何屬性。

## 建立 儲存貯體
<a name="using-s3-commands-managing-buckets-creating"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html) 命令來建立儲存貯體。儲存貯體名稱必須是***全域***唯一 (在所有 Amazon S3 中都為唯一)，且應符合 DNS 標準。

儲存貯體名稱可包含小寫字母、數字、連字號和句號。儲存貯體名稱的開頭和結尾只能使用字母或數字，連字號或句號旁邊不能使用句號。

**語法**

```
$ aws s3 mb <target> [--options]
```

### s3 mb 範例
<a name="using-s3-commands-managing-buckets-creating-examples"></a>

下列範例會建立 `s3://amzn-s3-demo-bucket` 儲存貯體。

```
$ aws s3 mb s3://amzn-s3-demo-bucket
```

## 列出儲存貯體和物件
<a name="using-s3-commands-listing-buckets"></a>

若要列出儲存貯體、資料夾或物件，請使用 [https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html) 命令。使用沒有目標或選項的命令會列出所有儲存貯體。

**語法**

```
$ aws s3 ls <target> [--options]
```

如需搭配此命令使用的幾個常見選項和範例，請參閱 [s3 命令的常用選項](#using-s3-commands-managing-objects-param)。如需可用選項的完整清單，請參閱 *AWS CLI 命令參考*中的 [https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html)。

### s3 ls 範例
<a name="using-s3-commands-managing-objects-list-examples"></a>

以下範例列出您的所有 Amazon S3 儲存貯體。

```
$ aws s3 ls
2018-12-11 17:08:50 amzn-s3-demo-bucket1
2018-12-14 14:55:44 amzn-s3-demo-bucket2
```

下列命令列出儲存貯體中的所有物件和前綴。在此範例輸出中，前綴 `example/` 有一個名為 `MyFile1.txt` 的檔案。

```
$ aws s3 ls s3://amzn-s3-demo-bucket
                           PRE example/
2018-12-04 19:05:48          3 MyFile1.txt
```

您可以將特定的前綴包含在命令中，將輸出篩選為該前綴。下列命令列出 *bucket-name/example/* 中的物件 (也就是使用前綴 *example/* 篩選出 *bucket-name* 中的物件)。

```
$ aws s3 ls s3://amzn-s3-demo-bucket/example/
2018-12-06 18:59:32          3 MyFile1.txt
```

若要僅顯示特定區域中的儲存貯體和物件，請使用 `--region` 選項

```
$ aws s3 ls --region us-east-2
2018-12-06 18:59:32          3 MyFile1.txt
```

如果您有大量的儲存貯體和物件清單，您可以使用 `--max-items` 或 `--page-size` 選項分頁結果。`--max-items` 選項會限制通話中傳回的總儲存貯體和物件數量，而 `--page-size` 選項會限制頁面上列出的儲存貯體和物件數量。

```
$ aws s3 ls --max-items 100 --page-size 10
```

如需分頁的詳細資訊，請參閱 [如何使用 --page-size 參數](cli-usage-pagination.md#cli-usage-pagination-pagesize) 和 [如何使用 --max-items 參數](cli-usage-pagination.md#cli-usage-pagination-maxitems)。

## 刪除儲存貯體
<a name="using-s3-commands-delete-buckets"></a>

若要刪除儲存貯體，請使用 [https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html) 命令。

**語法**

```
$ aws s3 rb <target> [--options]
```

### s3 rb 範例
<a name="using-s3-commands-removing-buckets-examples"></a>

以下範例會移除 `s3://amzn-s3-demo-bucket` 儲存貯體。

```
$ aws s3 rb s3://amzn-s3-demo-bucket
```

根據預設，儲存貯體必須為空才能成功操作。若要移除不是空的儲存貯體，您需要包含 `--force` 選項。如果您使用受版本控制的儲存貯體 (其包含先前已刪除但仍保留的物件)，則此命令*不會*允許您移除該儲存貯體。您必須先刪除所有內容。

以下範例命令會在儲存貯體中刪除所有物件和前綴，然後刪除該儲存貯體。

```
$ aws s3 rb s3://amzn-s3-demo-bucket --force
```

## 刪除物件
<a name="using-s3-commands-delete-objects"></a>

若要刪除儲存貯體或本機目錄中的物件，請使用 [https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html) 命令。

**語法**

```
$ aws s3 rm  <target> [--options]
```

如需搭配此命令使用的幾個常見選項和範例，請參閱 [s3 命令的常用選項](#using-s3-commands-managing-objects-param)。如需選項的完整清單，請參閱 *AWS CLI 命令參考*中的 [https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html)。

### s3 rm 範例
<a name="using-s3-commands-delete-objects-examples"></a>

下列範例刪除來自 `s3://amzn-s3-demo-bucket/example` 的 `filename.txt`。

```
$ aws s3 rm s3://amzn-s3-demo-bucket/example/filename.txt
```

以下範例使用 `--recursive` 選項刪除所有來自 `s3://amzn-s3-demo-bucket/example` 的物件。

```
$ aws s3 rm s3://amzn-s3-demo-bucket/example --recursive
```

## 移動物件
<a name="using-s3-commands-managing-objects-move"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html) 命令從儲存貯體或本機目錄移動物件。`s3 mv` 命令會將來源物件或檔案複製到指定的目的地，然後刪除來源物件或檔案。

**語法**

```
$ aws s3 mv <source> <target> [--options]
```

如需搭配此命令使用的幾個常見選項和範例，請參閱 [s3 命令的常用選項](#using-s3-commands-managing-objects-param)。如需可用選項的完整清單，請參閱 *AWS CLI 命令參考*中的 [https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html)。

**警告**  
如果您在 Amazon S3 來源或目的地 URI 中使用任何類型的存取點 ARN 或存取點別名，您必須特別注意來源和目的地 Amazon S3 URI 會解析為不同的基礎儲存貯體。使用來源和目的地儲存貯體相同，可以將來源檔案或物件移至其本身，這可能會導致意外刪除來源檔案或物件。若要驗證來源和目的地儲存貯體是不同的，請使用 `--validate-same-s3-paths` 參數，或將環境變數 ``AWS_CLI_S3_MV_VALIDATE_SAME_S3_PATHS`` 設定為 `true`。

### s3 mv 範例
<a name="using-s3-commands-managing-objects-move-examples"></a>

下列範例會將所有物件從 `s3://amzn-s3-demo-bucket/example` 移動至 `s3://amzn-s3-demo-bucket/`。

```
$ aws s3 mv s3://amzn-s3-demo-bucket/example s3://amzn-s3-demo-bucket/
```

以下範例會使用 `s3 mv` 命令將本機檔案從目前的工作目錄移動至 Amazon S3 儲存貯體。

```
$ aws s3 mv filename.txt s3://amzn-s3-demo-bucket
```

以下範例將檔案從 Amazon S3 儲存貯體移動至目前的工作目錄，其中 `./` 指定您目前使用中的目錄。

```
$ aws s3 mv s3://amzn-s3-demo-bucket/filename.txt ./
```

## 複製物件
<a name="using-s3-commands-managing-objects-copy"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) 命令從儲存貯體或本機目錄複製物件。

**語法**

```
$ aws s3 cp <source> <target> [--options]
```

您可以使用破折號參數將檔案串流傳輸至標準輸入 (`stdin`) 或標準輸出 (`stdout`)。

**警告**  
如果您使用的是 PowerShell，Shell 可能會改變 CRLF 的編碼，或者將 CRLF 新增至管道輸入或輸出，或者重新導向的輸出。

此 `s3 cp` 命令使用以下語法將檔案串流從 `stdin` 上傳至指定的儲存貯體。

**語法**

```
$ aws s3 cp - <target> [--options]
```

此 `s3 cp` 命令會使用下列語法來為 `stdout` 下載 Amazon S3 檔案串流。

**語法**

```
$ aws s3 cp <target> [--options] -
```

如需搭配此命令使用的幾個常見選項和範例，請參閱 [s3 命令的常用選項](#using-s3-commands-managing-objects-param)。如需選項的完整清單，請參閱 *AWS CLI 命令參考*中的 [https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html)。

### `s3 cp` 範例
<a name="using-s3-commands-managing-objects-copy-examples"></a>

下列範例會將來自 `s3://amzn-s3-demo-bucket/example` 的物件複製到 `s3://amzn-s3-demo-bucket/`。

```
$ aws s3 cp s3://amzn-s3-demo-bucket/example s3://amzn-s3-demo-bucket/
```

以下範例會使用 `s3 cp` 命令將本機檔案從目前的工作目錄複製至 Amazon S3 儲存貯體。

```
$ aws s3 cp filename.txt s3://amzn-s3-demo-bucket
```

以下範例將檔案從您的 Amazon S3 儲存貯體複製至目前的工作目錄，其中 `./` 指定您目前使用中的目錄。

```
$ aws s3 cp s3://amzn-s3-demo-bucket/filename.txt ./
```

以下範例會使用 Echo 將文字「Hello World」串流至 `s3://bucket-name/filename.txt` 檔案。

```
$ echo "hello world" | aws s3 cp - s3://amzn-s3-demo-bucket/filename.txt
```

下列範例會串流 `s3://amzn-s3-demo-bucket/filename.txt` 檔案至 `stdout` 並將內容列印至主控台。

```
$ aws s3 cp s3://amzn-s3-demo-bucket/filename.txt -
hello world
```

下列範例會串流 `s3://bucket-name/pre` 內容至 `stdout`，使用 `bzip2` 命令來壓縮檔案，再上傳名為 `key.bz2` 的壓縮檔至 `s3://bucket-name`。

```
$ aws s3 cp s3://amzn-s3-demo-bucket/pre - | bzip2 --best | aws s3 cp - s3://amzn-s3-demo-bucket/key.bz2
```

## 同步物件
<a name="using-s3-commands-managing-objects-sync"></a>

[https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html](https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html) 命令可同步儲存貯體和目錄的內容，或同步兩個儲存貯體的內容。一般來說，`s3 sync` 會複製遺失或過期的檔案，或是來源與目標之間的物件。但是您也可以提供 `--delete` 選項，來從目標中移除沒有出現在來源中的檔案或物件。

**語法**

```
$ aws s3 sync <source> <target> [--options]
```

如需搭配此命令使用的幾個常見選項和範例，請參閱 [s3 命令的常用選項](#using-s3-commands-managing-objects-param)。如需選項的完整清單，請參閱 *AWS CLI 命令參考*中的 [https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html](https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html)。

### s3 同步範例
<a name="using-s3-commands-managing-objects-sync-examples"></a>

下列範例將名為 *amzn-s3-demo-bucket* 的儲存貯體中名為 *path* 的 Amazon S3 前綴內容與目前的工作目錄進行同步。

`s3 sync` 會更新任何與目的地檔案具有相同名字，但是檔案大小或修改時間不同的檔案。輸出顯示在同步期間所執行的特定操作。請注意，操作會使用 `s3://amzn-s3-demo-bucket/path/MySubdirectory` 遞迴同步處理子目錄 `MySubdirectory` 及其內容。

```
$ aws s3 sync . s3://amzn-s3-demo-bucket/path
upload: MySubdirectory\MyFile3.txt to s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt
upload: MyFile2.txt to s3://amzn-s3-demo-bucket/path/MyFile2.txt
upload: MyFile1.txt to s3://amzn-s3-demo-bucket/path/MyFile1.txt
```

下列範例 (為前一個範例的延伸) 示範如何使用 `--delete` 選項。

```
// Delete local file
$ rm ./MyFile1.txt

// Attempt sync without --delete option - nothing happens
$ aws s3 sync . s3://amzn-s3-demo-bucket/path

// Sync with deletion - object is deleted from bucket
$ aws s3 sync . s3://amzn-s3-demo-bucket/path --delete
delete: s3://amzn-s3-demo-bucket/path/MyFile1.txt

// Delete object from bucket
$ aws s3 rm s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt
delete: s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt

// Sync with deletion - local file is deleted
$ aws s3 sync s3://amzn-s3-demo-bucket/path . --delete
delete: MySubdirectory\MyFile3.txt

// Sync with Infrequent Access storage class
$ aws s3 sync . s3://amzn-s3-demo-bucket/path --storage-class STANDARD_IA
```

在使用 `--delete` 選項時，`--exclude` 和 `--include` 選項可以在 `s3 sync` 操作期間篩選要刪除的檔案或物件。在此情況下，參數字串必須針對目標目錄或儲存貯體，指定檔案要排除或包含在刪除操作中。下列顯示一個範例。

```
Assume local directory and s3://amzn-s3-demo-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://amzn-s3-demo-bucket/path --delete --exclude "path/MyFile?.txt"
delete: s3://amzn-s3-demo-bucket/path/MyFile88.txt
'''

// Sync with delete, excluding MyFile2.rtf - local file is NOT deleted
$ aws s3 sync s3://amzn-s3-demo-bucket/path . --delete --exclude "./MyFile2.rtf"
download: s3://amzn-s3-demo-bucket/path/MyFile1.txt to MyFile1.txt
'''

// Sync with delete, local copy of MyFile2.rtf is deleted
$ aws s3 sync s3://amzn-s3-demo-bucket/path . --delete
delete: MyFile2.rtf
```

## s3 命令的常用選項
<a name="using-s3-commands-managing-objects-param"></a>

本主題中描述的指令經常使用下列選項。如需可在命令上使用的選項完整清單，請參閱第 [AWS CLI 2 版參考指南](https://docs.aws.amazon.com/cli/latest/reference/index.html)中的特定命令。

**acl**  
`s3 sync` 和 `s3 cp` 可以使用 `--acl` 選項。這可讓您為複製至 Amazon S3 的檔案設定存取許可。`--acl` 選項接受 `private`、`public-read` 和 `public-read-write` 值。如需詳細資訊，請參閱《Amazon S3 使用者指南》**中的[固定的 ACL](https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl)。  

```
$ aws s3 sync . s3://amzn-s3-demo-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://amzn-s3-demo-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://amzn-s3-demo-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://amzn-s3-demo-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://amzn-s3-demo-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://amzn-s3-demo-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://amzn-s3-demo-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\$1Type* – 指定識別被授予者的方式。可設定為 `uri`、`emailaddress` 或 `id`。
+ *Grantee\$1ID* – 根據 *Grantee\$1Type* 指定被授予者。
  + `uri` – 群組的 URI。如需詳細資訊，請參閱[被授予者是什麼？](https://docs.aws.amazon.com/AmazonS3/latest/userguide/ACLOverview.html#SpecifyingGrantee)
  + `emailaddress` – 帳戶的電子郵件地址。
  + `id` – 帳戶的正式 ID。
如需有關 Amazon S3 存取控制的詳細資訊，請參閱[存取控制](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingAuthAccess.html)。  
下列範例將物件複製到儲存貯體中。其授予所有人對該物件的 `read` 許可，並授予和 `full` 相關聯的帳戶對該物件的 `read` 取可 (`readacl`、`writeacl` 和 `user@example.com`)。  

```
$ aws s3 cp file.txt s3://amzn-s3-demo-bucket/ --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers full=emailaddress=user@example.com
```
您也可以為您上傳到 Amazon S3 的物件，指定非預設儲存方案 (`REDUCED_REDUNDANCY` 或 `STANDARD_IA`)。若要這麼做，請使用 `--storage-class` 選項。  

```
$ aws s3 cp file.txt s3://amzn-s3-demo-bucket/ --storage-class REDUCED_REDUNDANCY
```

**無覆寫**  
`s3 cp`、 `s3 mv`和 `s3 sync`命令包含一個`--no-overwrite`選項，您可以用來防止覆寫已存在於目的地的物件。  
只有當物件不存在於本機目錄中時，下列範例才會將物件從儲存貯體複製到本機目錄。  

```
$ aws s3 cp --no-overwrite s3://amzn-s3-demo-bucket/file.txt file.txt
```
下列範例會以遞迴方式將檔案從本機目錄複製到儲存貯體。它只會複製儲存貯體中尚不存在的檔案。  

```
$ aws s3 cp --recursive --no-overwrite /path/to/demo-files/ s3://amzn-s3-demo-bucket/demo-files/
```
下列範例只有在物件不存在於儲存貯體目的地位置時，才會將物件從本機目錄移至儲存貯體。  

```
$ aws s3 mv --no-overwrite file.txt s3://amzn-s3-demo-bucket/file.txt
```
下列範例會將檔案從本機目錄同步至儲存貯體。它只會同步目的地儲存貯體中尚不存在的檔案。  

```
$ aws s3 sync --no-overwrite /path/to/demo-files/ s3://amzn-s3-demo-bucket/demo-files/
```

**recursive**  
當您使用此選項時，會對指定目錄或前綴下的所有檔案或物件執行該命令。以下範例刪除 `s3://amzn-s3-demo-bucket/path` 及其所有內容。  

```
$ aws s3 rm s3://amzn-s3-demo-bucket/path --recursive
```

## Resources
<a name="using-s3-commands-managing-buckets-references"></a>

**AWS CLI 參考：**
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html](https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html)

**服務參考：**
+ 《Amazon S3 使用者指南》**中的[使用 Amazon S3 儲存貯體](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingBucket.html)。
+ 《Amazon S3 使用者指南》**中的[使用 Amazon S3 物件](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingObjects.html)。
+ 《Amazon S3 使用者指南》**中的[使用前綴和分隔符號以階層方式列出金鑰](https://docs.aws.amazon.com//AmazonS3/latest/userguide/ListingKeysHierarchy.html)
+ 使用《Amazon[ S3 使用者指南》中的 適用於 .NET 的 AWS SDK （低階） 中止分段上傳至 S3 儲存貯](https://docs.aws.amazon.com//AmazonS3/latest/userguide/LLAbortMPUnet.html)體 *Amazon S3 *

# 在 中使用 API 層級 (s3api) 命令 AWS CLI
<a name="cli-services-s3-apicommands"></a>

API 層級命令 (包含在 `s3api` 命令集中) 提供對 Amazon Simple Storage Service (Amazon S3) API 的直接存取，並啟用在高階 `s3` 命令中未公開的部分操作。這些命令等同於對服務功能提供 API 層級存取的其他 AWS 服務。如需這些 `s3` 命令的詳細資訊，請參閱 [在 中使用高階 (s3) 命令 AWS CLI](cli-services-s3-commands.md)

本主題提供範例，示範如何使用映射到 Amazon S3 API 的較低層級命令。此外，您可以在第 [AWS CLI 2 版參考指南](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html)的 `s3api`區段中找到每個 S3 API 命令的範例。

**Topics**
+ [先決條件](#cli-services-s3-apicommands-prereqs)
+ [套用自訂 ACL](#cli-services-s3-apicommands-acls)
+ [設定記錄政策](#cli-services-s3-apicommands-logpol)
+ [Resources](#cli-services-s3-apicommands-resources)

## 先決條件
<a name="cli-services-s3-apicommands-prereqs"></a>

若要執行 `s3api` 命令，您需要：
+ 安裝及設定 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)及[的身分驗證和存取憑證 AWS CLI](cli-chap-authentication.md)。
+ 您使用的設定檔必須具有許可，以允許範例執行 AWS 的操作。
+ 了解這些 Amazon S3 術語：
  + **儲存貯體** – 頂層 Amazon S3 資料夾。
  + **前綴** – 儲存貯體中的 Amazon S3 資料夾。
  + **物件** – 託管於 Amazon S3 儲存貯體中的任一個項目。

## 套用自訂 ACL
<a name="cli-services-s3-apicommands-acls"></a>

透過高階命令，您可使用 `--acl` 選項在 Amazon S3 物件上套用預先定義的存取控制清單 (ACL)。但不能使用該命令來設定整個儲存貯體的 ACL。不過，您可以使用 ```[put-bucket-acl](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-acl.html)` API 層級命令做到這一點。

下列範例顯示如何將完全控制權授予兩個 AWS 使用者 (*user1@example.com* 和 *user2@example.com*)，以及授予每個人讀取許可。「everyone」的識別符來自您當做參數傳遞的特殊 URI。

```
$ aws s3api put-bucket-acl --bucket amzn-s3-demo-bucket --grant-full-control 'emailaddress="user1@example.com",emailaddress="user2@example.com"' --grant-read 'uri="http://acs.amazonaws.com/groups/global/AllUsers"'
```

如需有關如何建構 ACL 的詳細資訊，請參閱《Amazon Simple Storage Service API 參考》**中的 [PUT 儲存貯體 acl](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTacl.html)。CLI 中的 `s3api` ACL 命令 (如 `put-bucket-acl`) 使用相同的[速記參數表示法](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-shorthand.html)。

## 設定記錄政策
<a name="cli-services-s3-apicommands-logpol"></a>

API 命令 `put-bucket-logging` 設定儲存貯體記錄政策。

在下列範例中， AWS 使用者 *user@example.com* 被授予對日誌檔案的完全控制權，而且所有使用者都可以讀取這些檔案。請注意，授予 Amazon S3 記錄傳送系統 (由 URI 指定) 將日誌讀取和寫入至儲存貯體的必要許可時，也需要 `put-bucket-acl` 命令。

```
$ aws s3api put-bucket-acl --bucket amzn-s3-demo-bucket --grant-read-acp 'URI="http://acs.amazonaws.com/groups/s3/LogDelivery"' --grant-write 'URI="http://acs.amazonaws.com/groups/s3/LogDelivery"'
$ aws s3api put-bucket-logging --bucket amzn-s3-demo-bucket --bucket-logging-status file://logging.json
```

上一個命令中的 `logging.json` 檔案包含下列內容。

```
{
  "LoggingEnabled": {
    "TargetBucket": "amzn-s3-demo-bucket",
    "TargetPrefix": "amzn-s3-demo-bucketLogs/",
    "TargetGrants": [
      {
        "Grantee": {
          "Type": "AmazonCustomerByEmail",
          "EmailAddress": "user@example.com"
        },
        "Permission": "FULL_CONTROL"
      },
      {
        "Grantee": {
          "Type": "Group",
          "URI": "http://acs.amazonaws.com/groups/global/AllUsers"
        },
        "Permission": "READ"
      }
    ]
  }
}
```

## Resources
<a name="cli-services-s3-apicommands-resources"></a>

**AWS CLI 參考：**
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-acl.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-acl.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-logging.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-logging.html)

**服務參考：**
+ 《Amazon S3 使用者指南》**中的[使用 Amazon S3 儲存貯體](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingBucket.html)。
+ 《Amazon S3 使用者指南》**中的[使用 Amazon S3 物件](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingObjects.html)。
+ 《Amazon S3 使用者指南》**中的[使用前綴和分隔符號以階層方式列出金鑰](https://docs.aws.amazon.com//AmazonS3/latest/userguide/ListingKeysHierarchy.html)
+ 使用《Amazon[ S3 使用者指南》中的 適用於 .NET 的 AWS SDK （低階） 中止分段上傳至 S3 儲存貯](https://docs.aws.amazon.com//AmazonS3/latest/userguide/LLAbortMPUnet.html)體 *Amazon S3 *

# 中的 Amazon S3 儲存貯體生命週期指令碼範例 AWS CLI
<a name="cli-services-s3-lifecycle-example"></a>

本主題使用 Bash 指令碼範例來說明使用 AWS Command Line Interface (AWS CLI) 的 Amazon S3 儲存貯體生命週期操作。此指令碼範例會使用 [https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html) 命令組。Shell 指令碼是專為在命令列界面中執行而設計的程式。

**Topics**
+ [開始之前](#cli-services-s3-lifecycle-example-before)
+ [關於此範例](#cli-services-s3-lifecycle-example-about)
+ [檔案](#cli-services-s3-lifecycle-example-files)
+ [參考](#cli-services-s3-lifecycle-example-references)

## 開始之前
<a name="cli-services-s3-lifecycle-example-before"></a>

在可以執行下列任何範例之前，您必須先完成下列事項。
+ 安裝及設定 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)及[的身分驗證和存取憑證 AWS CLI](cli-chap-authentication.md)。
+ 您使用的設定檔必須具有許可，以允許範例執行 AWS 的操作。
+  AWS 最佳實務是授予此程式碼的最低權限，或僅授予執行任務所需的許可。如需詳細資訊，請參閱*《IAM 使用者指南》*中的[授予最低權限](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)。
+ 此程式碼尚未在所有 AWS 區域中進行測試。某些 AWS 服務僅適用於特定 區域。如需詳細資訊，請參閱《AWS 一般參考指南》**中的[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)。
+ 執行此程式碼可能會導致 AWS 您的帳戶產生費用。您有責任確保在使用完該指令碼建立的所有資源後將這些資源移除。

Amazon S3 服務使用以下術語：
+ 儲存貯體 – 頂層 Amazon S3 資料夾。
+ 前綴 – 儲存貯體中的 Amazon S3 資料夾。
+ 物件 – 託管於 Amazon S3 儲存貯體中的任一個項目。

## 關於此範例
<a name="cli-services-s3-lifecycle-example-about"></a>

此範例示範如何使用 Shell 指令碼檔案中的一組函數與某些基本 Amazon S3 操作互動。這些函數位於名為 `bucket-operations.sh` 的 Shell 指令碼檔案中。您可以在另一個檔案中呼叫這些函數。每個指令碼檔案包含描述每個函數的註釋。

若要查看每個步驟的中繼結果，請執行具有 `-i` 參數的指令碼。您可以使用 Amazon S3 主控台檢視儲存貯體的目前狀態或其內容。在出現提示時按下 **ENTER** 後，指令碼才會繼續執行下一個步驟。

如需完整範例和可下載的指令碼檔案，請參閱 *GitHub* 上 *AWS 程式碼範例儲存庫*中的 [Amazon S3 儲存貯體生命週期操作](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations)。

## 檔案
<a name="cli-services-s3-lifecycle-example-files"></a>

本範例內含下列檔案：

**bucket-operations.sh**  
此主指令碼檔案可從另一個檔案獲取。它包含執行以下任務的函數：  
+ 建立一個儲存貯體並驗證它是否存在
+ 將檔案從本機電腦複製至儲存貯體
+ 將檔案從一個儲存貯體位置複製到其他儲存貯體位置
+ 列出儲存貯體的內容
+ 從儲存貯體刪除檔案
+ 刪除儲存貯體
檢視 *GitHub* 上的 `[bucket-operations.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/bucket_operations.sh)`。

**test-bucket-operations.sh**  
Shell 指令碼檔案 `test-bucket-operations.sh` 示範了如何透過獲取 `bucket-operations.sh` 檔案並呼叫每個函數來呼叫函數。呼叫函數後，測試指令碼會刪除它所建立的所有資源。  
檢視 *GitHub* 上的 `[test-bucket-operations.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/test_bucket_operations.sh)`。

**awsdocs-general.sh**  
指令碼檔案 `awsdocs-general.sh` 會保存在各個 AWS CLI進階程式碼範例中使用的一般用途函數。  
檢視 *GitHub* 上的 `[awsdocs-general.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/awsdocs_general.sh)`。

## 參考
<a name="cli-services-s3-lifecycle-example-references"></a>

**AWS CLI 參考：**
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/create-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/create-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html)

**其他參考：**
+ 《Amazon S3 使用者指南》**中的[使用 Amazon S3 儲存貯體](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingBucket.html)。
+ 《Amazon S3 使用者指南》**中的[使用 Amazon S3 物件](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingObjects.html)。
+ 若要檢視和貢獻 AWS SDK 和 AWS CLI 程式碼範例，請參閱 *GitHub* 上的[AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/)。