

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

# Amazon S3 儲存貯體政策的範例
<a name="example-bucket-policies"></a>

使用 Amazon S3 儲存貯體政策，您可以安全地存取儲存貯體中的物件，以便只有具有適當許可的使用者才能存取它們。您甚至可以防止已驗證但沒有適當許可的使用者存取 Amazon S3 資源。

本節顯示儲存貯體政策之一般使用案例的範例。這些範例策略會使用 `amzn-s3-demo-bucket` 作為資源值。若要測試這些政策，請將 `user input placeholders` 取代為您自己的資訊 (例如儲存貯體名稱)。

若要授予或拒絶一組物件的許可，您可以在 Amazon Resource Name (ARN) 和其他值上使用萬用字元 (`*`)。例如，您可以控制對開頭為通用[字首](https://docs.aws.amazon.com/general/latest/gr/glos-chap.html#keyprefix)或結尾為特定副檔名 (例如 `.html`) 之物件群組的存取。

如需 AWS Identity and Access Management (IAM) 政策語言的詳細資訊，請參閱 [Amazon S3 中的政策和許可](access-policy-language-overview.md)。

如需依 S3 資源類型列出 S3 API 操作許可的詳細資訊，請參閱[Amazon S3 API 操作所需的許可](using-with-s3-policy-actions.md)。

**注意**  
使用 Amazon S3 主控台來測試許可時，您必須授予主控台所需的其他許可：`s3:ListAllMyBuckets`、`s3:GetBucketLocation` 和 `s3:ListBucket`。如需授予使用者許可並使用主控台測試這些許可的演練範例，請參閱 [使用使用者政策來控制對儲存貯體的存取](walkthrough1.md)。

建立儲存貯體政策的其他資源包括：
+ 如需建立儲存貯體政策時可以使用的 IAM 政策動作、資源和條件索引鍵清單，請參閱 服務授權參考**中的 [Amazon S3 的動作、資源和條件索引鍵](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazons3.html)。
+ 如需依 S3 資源類型列出 S3 API 操作許可的詳細資訊，請參閱[Amazon S3 API 操作所需的許可](using-with-s3-policy-actions.md)。
+ 如需建立 S3 政策的指引，請參閱 [使用 Amazon S3 主控台新增儲存貯體政策](add-bucket-policy.md)。
+ 若要對政策錯誤進行故障排除，請參閱 [對 Amazon S3 中的存取遭拒 (403 Forbidden) 錯誤進行故障診斷](troubleshoot-403-errors.md)。

如果您在新增或更新政策時遇到問題，請參閱[當我嘗試更新 Amazon S3 儲存貯體政策時，為什麼會收到錯誤「政策中的主體無效」？](https://repost.aws/knowledge-center/s3-invalid-principal-in-policy-error) AWS re:Post 知識中心的 。

**Topics**
+ [將唯讀許可授予公有匿名使用者](#example-bucket-policies-anonymous-user)
+ [需要加密](#example-bucket-policies-encryption)
+ [使用標準 ACL 管理儲存貯體](#example-bucket-policies-public-access)
+ [使用物件標記來管理物件存取](#example-bucket-policies-object-tags)
+ [使用全域條件金鑰管理物件存取](#example-bucket-policies-global-condition-keys)
+ [根據 HTTP 或 HTTPS 請求管理存取](#example-bucket-policies-HTTP-HTTPS)
+ [管理使用者對特定資料夾的存取](#example-bucket-policies-folders)
+ [管理存取日誌的存取](#example-bucket-policies-access-logs)
+ [管理對 Amazon CloudFront OAI 的存取](#example-bucket-policies-cloudfront)
+ [管理對 Amazon S3 Storage Lens 的存取](#example-bucket-policies-lens)
+ [管理 S3 庫存、S3 分析和 S3 庫存報告的許可](#example-bucket-policies-s3-inventory)
+ [需要 MFA](#example-bucket-policies-MFA)
+ [防止使用者刪除物件](#using-with-s3-actions-related-to-bucket-subresources)

## 將唯讀許可授予公有匿名使用者
<a name="example-bucket-policies-anonymous-user"></a>

您可以使用政策設定將存取權授予公有匿名使用者，假如您要將儲存貯體設定為靜態網站，這樣做會很有幫助。將存取權授予公有匿名使用者時，您必須停用儲存貯體的封鎖公開存取設定。如需如何執行這項操作的詳細資訊，請參閱[設定網站存取許可](WebsiteAccessPermissionsReqd.md)。若要了解如何為相同目的設定更嚴格的政策，請參閱[如何將公開讀取存取權授予 Amazon S3 儲存貯體中的某些物件？](https://repost.aws/knowledge-center/read-access-objects-s3-bucket) AWS 知識中心的 。

根據預設，Amazon S3 會封鎖對帳戶和儲存貯體的公開存取。如想要使用儲存貯體託管靜態網站，您可使用這些步驟編輯封鎖公有存取設定：

**警告**  
完成這些步驟之前，請檢閱[封鎖對 Amazon S3 儲存體的公開存取權](access-control-block-public-access.md)以確保您了解並接受允許公開存取所涉及的風險。當您關閉封鎖公開存取設定以公開儲存貯體時，網際網路上的任何人都可以存取您的儲存貯體。我們建議您封鎖對儲存貯體的所有公用存取權。

1. 開啟位於 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 的 Amazon S3 主控台。

1. 選擇已設定為靜態網站的儲存貯體名稱。

1. 選擇 **Permissions** (許可)。

1. 在 **Block public access (bucket settings) (封鎖公開存取 (儲存貯體設定))** (封鎖公開存取 (儲存貯體設定)) 下，選擇 **Edit** (編輯)。

1. 清除 **Block *all* public access** (封鎖所有公開存取)，然後選擇 **Save changes** (儲存變更)。  
![\[Amazon S3 主控台，顯示封鎖公開存取儲存貯體設定。\]](http://docs.aws.amazon.com/zh_tw/AmazonS3/latest/userguide/images/edit-public-access-clear.png)

   Amazon S3 會關閉儲存貯體的封鎖公開存取設定。若要建立公有靜態網站，在新增儲存貯體原則之前，可能還需要針對您的帳戶[編輯封鎖公開存取設定](https://docs.aws.amazon.com/AmazonS3/latest/user-guide/block-public-access-account.html)。如果目前已開啟帳戶的封鎖公開存取設定，您會在**封鎖公開存取 (儲存貯體設定)** 下看到附註。

## 需要加密
<a name="example-bucket-policies-encryption"></a>

您可以使用 AWS Key Management Service (AWS KMS) 金鑰 (SSE-KMS) 要求伺服器端加密，如下列範例所示。

### 寫入儲存貯體的所有物件都需要 SSE-KMS
<a name="example-bucket-policies-encryption-1"></a>

下列範例政策要求寫入儲存貯體的每個物件都必須使用 AWS Key Management Service (AWS KMS) 金鑰 (SSE-KMS) 進行伺服器端加密。如果物件未使用 SSE-KMS 加密，則會拒絕請求。

------
#### [ JSON ]

****  

```
{
"Version":"2012-10-17",		 	 	 
"Id": "PutObjPolicy",
"Statement": [{
  "Sid": "DenyObjectsThatAreNotSSEKMS",
  "Principal": "*",
  "Effect": "Deny",
  "Action": "s3:PutObject",
  "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*",
  "Condition": {
    "Null": {
      "s3:x-amz-server-side-encryption-aws-kms-key-id": "true"
    }
  }
}]
}
```

------

### 對於寫入儲存貯體的所有物件，需要具有特定 AWS KMS key 的 SSE-KMS
<a name="example-bucket-policies-encryption-2"></a>

下列範例政策會拒絕任何物件寫入儲存貯體 (如果這些物件未使用特定的 KMS 金鑰 ID 搭配 SSE-KMS 加密的話)。即使物件使用每個請求標頭或儲存貯體預設加密搭配 SSE-KMS 加密，但如果尚未使用指定的 KMS 金鑰加密物件，這些物件也無法寫入儲存貯體。請務必將本範例中使用的 KMS 金鑰 ARN 取代為您自己的 KMS 金鑰 ARN。

------
#### [ JSON ]

****  

```
{
"Version":"2012-10-17",		 	 	 
"Id": "PutObjPolicy",
"Statement": [{
  "Sid": "DenyObjectsThatAreNotSSEKMSWithSpecificKey",
  "Principal": "*",
  "Effect": "Deny",
  "Action": "s3:PutObject",
  "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*",
  "Condition": {
    "ArnNotEqualsIfExists": {
      "s3:x-amz-server-side-encryption-aws-kms-key-id": "arn:aws:kms:us-east-1:111122223333:key/01234567-89ab-cdef-0123-456789abcdef"
    }
  }
}]
}
```

------

## 使用標準 ACL 管理儲存貯體
<a name="example-bucket-policies-public-access"></a>

### 將許可授予多個帳戶，以上傳物件或設定物件 ACL 進行公開存取
<a name="example-bucket-policies-acl-1"></a>

下列範例政策會將 `s3:PutObject` 和 `s3:PutObjectAcl` 許可授予多個 AWS 帳戶。此外，範例政策要求這些操作的任何請求都必須包含 `public-read` [標準存取控制清單 (ACL)](acl-overview.md#canned-acl)。如需詳細資訊，請參閱[Amazon S3 的政策動作](security_iam_service-with-iam.md#security_iam_service-with-iam-id-based-policies-actions)及[Amazon S3 的政策條件索引鍵](security_iam_service-with-iam.md#security_iam_service-with-iam-id-based-policies-conditionkeys)。

**警告**  
`public-read` 標準 ACL 可讓全世界的任何人檢視您儲存貯體中的物件。授予對 Amazon S3 bucket 儲存貯體的匿名存取，或停用封鎖公開存取設定時，請小心。當您授予匿名存取時，全球所有人皆可存取您的儲存貯體。我們建議您永遠不要授予匿名存取您的 Amazon S3 儲存貯體，除非您特別需要 (例如使用[靜態網站託管](WebsiteHosting.md)時)。如果您想要針對靜態網站託管啟用封鎖公開存取設定，請參閱[教學課程：在 Amazon S3 上設定靜態網站](https://docs.aws.amazon.com/AmazonS3/latest/userguide/HostingWebsiteOnS3Setup.html)。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AddPublicReadCannedAcl",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::111122223333:root",
                    "arn:aws:iam::444455556666:root"
                ]
            },
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*",
            "Condition": {
                "StringEquals": {
                    "s3:x-amz-acl": [
                        "public-read"
                    ]
                }
            }
        }
    ]
}
```

------

### 授予跨帳戶許可，以在確保儲存貯體擁有者具有完全控制時上傳物件
<a name="example-bucket-policies-acl-2"></a>

下列範例顯示如何允許另一個 AWS 帳戶 將物件上傳到您的儲存貯體，同時確保您完全控制上傳的物件。此政策授予特定 AWS 帳戶 (*`111122223333`*) 上傳物件的能力，前提是該帳戶在上傳時包含`bucket-owner-full-control`標準 ACL。政策中的 `StringEquals` 條件指定 `s3:x-amz-acl` 條件金鑰來表示標準 ACL 需求。如需詳細資訊，請參閱[Amazon S3 的政策條件索引鍵](security_iam_service-with-iam.md#security_iam_service-with-iam-id-based-policies-conditionkeys)。

------
#### [ JSON ]

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
     {
       "Sid":"PolicyForAllowUploadWithACL",
       "Effect":"Allow",
       "Principal":{"AWS":"111122223333"},
       "Action":"s3:PutObject",
       "Resource":"arn:aws:s3:::amzn-s3-demo-bucket/*",
       "Condition": {
         "StringEquals": {"s3:x-amz-acl":"bucket-owner-full-control"}
       }
     }
   ]
}
```

------

## 使用物件標記來管理物件存取
<a name="example-bucket-policies-object-tags"></a>

### 允許使用者只讀取具有特定索引標籤金鑰和值的物件
<a name="example-bucket-policies-tagging-1"></a>

下列許可政策會限制使用者只能讀取具有 `environment: production` 索引標籤金鑰和值的物件。此政策會使用 `s3:ExistingObjectTag` 條件金鑰來指定索引標籤金鑰和值。

------
#### [ JSON ]

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Principal":{
            "AWS":"arn:aws:iam::111122223333:role/JohnDoe"
         },
         "Effect":"Allow",
         "Action":[
            "s3:GetObject",
            "s3:GetObjectVersion"
         ],
         "Resource":"arn:aws:s3:::amzn-s3-demo-bucket/*",
         "Condition":{
            "StringEquals":{
               "s3:ExistingObjectTag/environment":"production"
            }
         }
      }
   ]
}
```

------

### 限制使用者可以新增哪些物件索引標籤金鑰
<a name="example-bucket-policies-tagging-2"></a>

下列範例政策授予使用者執行 `s3:PutObjectTagging` 動作的許可，允許使用者將索引標籤新增至現有的物件。此條件使用 `s3:RequestObjectTagKeys` 條件金鑰指定允許的索引標籤金鑰，例如 `Owner` 或 `CreationDate`。如需詳細資訊，請參閱《IAM 使用者指南》**中的[建立測試多個金鑰值的條件](https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_policies_multi-value-conditions.html)。

此政策可確保請求中指定的每個索引標籤金鑰都是授權的標籤金鑰。條件中的 `ForAnyValue` 限定詞可確保請求中至少會出現其中一個指定的金鑰。

------
#### [ JSON ]

****  

```
{
   "Version":"2012-10-17",		 	 	 
  "Statement": [
    {"Principal":{"AWS":[
            "arn:aws:iam::111122223333:role/JohnDoe"
         ]
       },
 "Effect": "Allow",
      "Action": [
        "s3:PutObjectTagging"
      ],
      "Resource": [
        "arn:aws:s3:::amzn-s3-demo-bucket/*"
      ],
      "Condition": {"ForAnyValue:StringEquals": {"s3:RequestObjectTagKeys": [
            "Owner",
            "CreationDate"
          ]
        }
      }
    }
  ]
}
```

------

### 允許使用者新增物件標籤時，需要特定索引標籤金鑰和值
<a name="example-bucket-policies-tagging-3"></a>

下列範例政策授予使用者執行 `s3:PutObjectTagging` 動作的許可，允許使用者將標籤新增至現有的物件。此條件需要使用者包括值設為 `X` 的特定索引標籤 (例如 `Project`)。

------
#### [ JSON ]

****  

```
{
   "Version":"2012-10-17",		 	 	 
  "Statement": [
    {"Principal":{"AWS":[
       "arn:aws:iam::111122223333:user/JohnDoe"
         ]
       },
      "Effect": "Allow",
      "Action": [
        "s3:PutObjectTagging"
      ],
      "Resource": [
        "arn:aws:s3:::amzn-s3-demo-bucket/*"
      ],
      "Condition": {"StringEquals": {"s3:RequestObjectTag/Project": "X"
        }
      }
    }
  ]
}
```

------

### 允許使用者只新增具有特定物件索引標籤金鑰和值的物件
<a name="example-bucket-policies-tagging-4"></a>

下列範例政策會授與使用者執行 `s3:PutObject` 動作的許可，讓他們可將物件新增至儲存貯體。不過，`Condition` 陳述式會限制所上載物件上允許的索引標籤金鑰和值。在此範例中，使用者只能將具有值設為 `Finance` 的特定索引標籤金鑰 (`Department`) 的物件新增至儲存貯體。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Principal":{
            "AWS":[
                 "arn:aws:iam::111122223333:user/JohnDoe"
         ]
        },
        "Effect": "Allow",
        "Action": [
            "s3:PutObject"
        ],
        "Resource": [
            "arn:aws:s3:::amzn-s3-demo-bucket/*"
        ],
        "Condition": {
            "StringEquals": {
                "s3:RequestObjectTag/Department": "Finance"
            }
        }
    }]
}
```

------

## 使用全域條件金鑰管理物件存取
<a name="example-bucket-policies-global-condition-keys"></a>

[全域條件索引](https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_policies_condition-keys.html)鍵是具有 `aws` 字首的條件內容索引鍵。 AWS 服務 可支援全域條件索引鍵或包含服務字首的服務特定索引鍵。您可以使用 JSON 政策的 `Condition` 元素，來比較請求中的金鑰和您在政策中指定的金鑰值。

### 限制只能存取 Amazon S3 伺服器存取日誌交付
<a name="example-bucket-policies-global-condition-keys-1"></a>

在下列範例儲存貯體政策中，[https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourcearn](https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourcearn) 全域條件金鑰，用來將提出服務對服務請求之資源的 [Amazon Resource Name (ARN)](https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_identifiers.html#identifiers-arns) 與您在政策中所指定的 ARN 進行比較。`aws:SourceArn` 全域條件金鑰用來防止 Amazon S3 服務在服務之間用作交易期間的[混淆代理人](https://docs.aws.amazon.com//IAM/latest/UserGuide/confused-deputy.html)。只允許 Amazon S3 服務將物件新增至 Amazon S3 儲存貯體。

此範例儲存貯體政策只向日誌記錄服務主體 (`logging.s3.amazonaws.com`) 授予 `s3:PutObject` 許可。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutObjectS3ServerAccessLogsPolicy",
            "Principal": {
                "Service": "logging.s3.amazonaws.com"
            },
            "Effect": "Allow",
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::amzn-s3-demo-bucket-logs/*",
            "Condition": {
                "StringEquals": {
                "aws:SourceAccount": "111122223333"
                },
                "ArnLike": {
                "aws:SourceArn": "arn:aws:s3:::amzn-s3-demo-source-bucket1"
                }
            }
        },
        {
            "Sid": "RestrictToS3ServerAccessLogs",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::amzn-s3-demo-bucket-logs/*",
            "Condition": {
                "ForAllValues:StringNotEquals": {
                    "aws:PrincipalServiceNamesList": "logging.s3.amazonaws.com"
                }
            }
        }
    ]
}
```

------

### 僅允許存取您的組織
<a name="example-bucket-policies-global-condition-keys-2"></a>

如果您想要要求存取資源的所有 [IAM 主體](https://docs.aws.amazon.com//IAM/latest/UserGuide/intro-structure.html#intro-structure-principal)來自組織中 AWS 帳戶 的 （包括 AWS Organizations 管理帳戶），您可以使用 `aws:PrincipalOrgID`全域條件金鑰。

若要授予或限制此類型的存取權，請在儲存貯體政策中定義 `aws:PrincipalOrgID` 條件，並將值設為您的[組織 ID](https://docs.aws.amazon.com//organizations/latest/userguide/orgs_manage_org_details.html)。組織 ID 是用來控制對儲存貯體的存取。當您使用 `aws:PrincipalOrgID` 條件時，來自儲存貯體政策的許可也會套用至新增至組織的所有新帳戶。

以下是資源型儲存貯體政策的範例，您可以使用此政策，授予組織中特定 IAM 主體存取儲存貯體的權限。透過將 `aws:PrincipalOrgID` 全域條件金鑰新增至儲存貯體政策，現在需要主體帳戶位於您的組織中，才能取得資源的存取權。即使您在授予存取權時意外指定了不正確的帳戶，[aws:PrincipalOrgID 全域條件金鑰](https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-principalorgid)也會作為額外的防護。當此全域金鑰用於政策時，其可防止指定組織以外的所有主體存取 S3 儲存貯體。只有來自所列組織中帳戶的主體才能取得資源的存取權。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "AllowGetObject",
        "Principal": {
            "AWS": "*"
        },
        "Effect": "Allow",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*",
        "Condition": {
            "StringEquals": {
                "aws:PrincipalOrgID": ["o-aa111bb222"]
            }
        }
    }]
}
```

------

## 根據 HTTP 或 HTTPS 請求管理存取
<a name="example-bucket-policies-HTTP-HTTPS"></a>

### 限制僅 HTTPS 請求才能存取
<a name="example-bucket-policies-use-case-HTTP-HTTPS-1"></a>

如果您想要防止潛在攻擊者操控網路流量，可以使用 HTTPS (TLS) 來僅允許加密連線，同時限制 HTTP 請求存取您的儲存貯體。若要判斷請求是 HTTP 還是 HTTPS，請在 S3 儲存貯體政策中使用 [https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-securetransport](https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-securetransport) 全域條件金鑰。`aws:SecureTransport` 條件金鑰會檢查是否已使用 HTTP 傳送請求。

如果請求傳回 `true`，則請求是透過 HTTPS 傳送。如果請求傳回 `false`，則請求是透過 HTTP 傳送。然後，您可以根據所需的請求配置來允許或拒絕對儲存貯體的存取。

在下列範例中，儲存貯體政策明確地拒絕 HTTP 請求。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "RestrictToTLSRequestsOnly",
        "Action": "s3:*",
        "Effect": "Deny",
        "Resource": [
            "arn:aws:s3:::amzn-s3-demo-bucket",
            "arn:aws:s3:::amzn-s3-demo-bucket/*"
        ],
        "Condition": {
            "Bool": {
                "aws:SecureTransport": "false"
            }
        },
        "Principal": "*"
    }]
}
```

------

### 限制對特定 HTTP 參照的存取
<a name="example-bucket-policies-HTTP-HTTPS-2"></a>

假設您擁有具備網域名稱 (*`www.example.com`* 或 *`example.com`*) 的網站，其中包含您儲存貯體 (名為 `amzn-s3-demo-bucket`) 中存放之照片和影片的連結。根據預設，所有 Amazon S3 資源都是私有的，因此只有 AWS 帳戶 建立資源的 才能存取它們。

若要允許網站中這些物件的讀取存取，您可以新增一個儲存貯體政策，允許條件為 `GET` 請求必須源自於特定網頁的 `s3:GetObject` 許可。下列政策會使用 `StringLike` 條件搭配 `aws:Referer` 條件金鑰來限制請求。

請確定您使用的瀏覽器在請求中包含 HTTP `referer` 標頭。

**警告**  
我們建議您使用 `aws:Referer` 條件金鑰時要小心。包含公開已知 HTTP 推薦者標頭值相當危險。未授權方可以使用修改的或自訂瀏覽器來提供他們選擇的任何 `aws:Referer` 值。因此，請勿使用 `aws:Referer` 來防止未經授權方提出直接 AWS 請求。  
`aws:Referer` 條件金鑰僅用於允許客戶保護其數位內容 (例如存放在 Amazon S3 中的內容)，使其不被未經授權的第三方網站參考。如需詳細資訊，請參閱《*IAM 使用者指南》*中的 [https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-referer](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-referer)。

## 管理使用者對特定資料夾的存取
<a name="example-bucket-policies-folders"></a>

### 授予使用者存取特定資料夾的權限
<a name="example-bucket-policies-folders-1"></a>

假設您正在嘗試授予使用者存取特定資料夾的權限。如果 IAM 使用者和 S3 儲存貯體屬於相同 AWS 帳戶，則您可以使用 IAM 政策授予使用者存取特定儲存貯體資料夾的權限。透過此方式，您不需要更新儲存貯體政策來授予存取權。您可以將 IAM 政策新增至多個使用者可切換的 IAM 角色。

如果 IAM 身分和 S3 儲存貯體屬於不同 AWS 帳戶，則您必須在 IAM 政策和儲存貯體政策中授予跨帳戶存取權。如需如何授予跨帳户存取權的詳細資訊，請參閱[授予跨帳戶儲存貯體許可的儲存貯體擁有者](https://docs.aws.amazon.com//AmazonS3/latest/userguide/example-walkthroughs-managing-access-example2.html)。

下列範例儲存貯體政策僅授予 `JohnDoe` 其資料夾 (`home/JohnDoe/`) 的完整主控台存取權。透過建立 `home` 資料夾並將適當的許可授予使用者，您可以讓多個使用者共用單一儲存貯體。此政策包含三個 `Allow` 陳述式：
+ `AllowRootAndHomeListingOfCompanyBucket`：允許使用者許 (`JohnDoe`) 列出 `amzn-s3-demo-bucket` 儲存貯體根層級和 `home` 資料夾中的物件。此陳述式還允許使用者透過使用主控台搜尋字首 `home/`。
+ `AllowListingOfUserFolder`：允許使用者 (`JohnDoe`) 列出 `home/JohnDoe/` 資料夾和任何子資料夾中的所有物件。
+ `AllowAllS3ActionsInUserFolder`：透過授予`Read`、`Write` 和 `Delete` 許可來允許使用者執行所有 Amazon S3 動作。許可限於儲存貯體擁有者的主資料夾。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowRootAndHomeListingOfCompanyBucket",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::111122223333:user/JohnDoe"
                ]
            },
            "Effect": "Allow",
            "Action": ["s3:ListBucket"],
            "Resource": ["arn:aws:s3:::amzn-s3-demo-bucket"],
            "Condition": {
                "StringEquals": {
                    "s3:prefix": ["", "home/", "home/JohnDoe"],
                    "s3:delimiter": ["/"]
                }
            }
        },
        {
            "Sid": "AllowListingOfUserFolder",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::111122223333:user/JohnDoe"
                ]
            },
            "Action": ["s3:ListBucket"],
            "Effect": "Allow",
            "Resource": ["arn:aws:s3:::amzn-s3-demo-bucket"],
            "Condition": {
                "StringLike": {
                    "s3:prefix": ["home/JohnDoe/*"]
                }
            }
        },
        {
            "Sid": "AllowAllS3ActionsInUserFolder",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::111122223333:user/JohnDoe"
                ]
            },
            "Action": ["s3:*"],
            "Resource": ["arn:aws:s3:::amzn-s3-demo-bucket/home/JohnDoe/*"]
        }
    ]
}
```

------

## 管理存取日誌的存取
<a name="example-bucket-policies-access-logs"></a>

### 授予 Application Load Balancer 的存取權以啟用存取
<a name="example-bucket-policies-access-logs-1"></a>

對 Application Load Balancer 啟用存取記錄時，您必須指定 S3 儲存貯體的名稱，負載平衡器將在其中[存放日誌](https://docs.aws.amazon.com//elasticloadbalancing/latest/application/enable-access-logging.html#access-log-create-bucket)。儲存貯體必須具有[附加的政策](https://docs.aws.amazon.com//elasticloadbalancing/latest/application/enable-access-logging.html#attach-bucket-policy)，以授予 Elastic Load Balancing 寫入儲存貯體的許可。

在下列範例中，儲存貯體政策會授予 Elastic Load Balancing (ELB) 將存取日誌寫入儲存貯體的許可：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Principal": {
                "AWS": "arn:aws:iam::111122223333:root"
            },
            "Effect": "Allow",
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/prefix/AWSLogs/111122223333/*"
        }
    ]
}
```

------

**注意**  
務必針對您的 AWS 區域將 `elb-account-id` 取代為 Elastic Load Balancing 的 AWS 帳戶 ID。如需 Elastic Load Balancing 區域的清單，請參閱《Elastic Load Balancing 使用者指南》**中的[將政策附加到 Amazon S3 儲存貯體](https://docs.aws.amazon.com//elasticloadbalancing/latest/classic/enable-access-logs.html#attach-bucket-policy)。

如果您的 AWS 區域 未出現在支援的 Elastic Load Balancing 區域清單中，請使用下列政策，將許可授予指定的日誌交付服務。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
       "Principal": {
         "Service": "logdelivery.elasticloadbalancing.amazonaws.com"
          },
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/prefix/AWSLogs/111122223333/*"
    }
  ]
}
```

------

然後，務必啟用 [Elastic Load Balancing 存取日誌](https://docs.aws.amazon.com//elasticloadbalancing/latest/application/enable-access-logging.html#enable-access-logs)來設定這些存取日誌。您可以建立測試檔案來[驗證儲存貯體許可](https://docs.aws.amazon.com//elasticloadbalancing/latest/application/enable-access-logging.html#verify-bucket-permissions)。

## 管理對 Amazon CloudFront OAI 的存取
<a name="example-bucket-policies-cloudfront"></a>

### 對 Amazon CloudFront OAI 授予許可
<a name="example-bucket-policies-cloudfront-1"></a>

下列儲存貯體政策範例對 CloudFront 原始存取身分 (OAI) 授予許可，以允許取得 (讀取) S3 儲存貯體中的所有物件。您可以使用 CloudFront OAI 來允許使用者透過 CloudFront 存取儲存貯體中的物件，而不是直接透過 Amazon S3 存取。如需詳細資訊，請參閱《Amazon CloudFront 開發人員指南》**中的[使用原始存取身分限制對 Amazon S3 內容的存取](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html)。

下列政策使用 OAI 的 ID 作為政策的 `Principal`。如需使用 S3 儲存貯體政策，對 CloudFront OAI 授予存取的詳細資訊，請參閱《Amazon CloudFront 開發人員指南》**中的[從原始存取身分 (OAI) 遷移至原始存取控制 (OAC)](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html#migrate-from-oai-to-oac)。

若要使用此範例：
+ 將 `EH1HDMB1FH2TC` 取代為 OAI 的 ID。若要尋找 OAI 的 ID，請參閱 CloudFront 主控台上的[原始存取身分頁面](https://console.aws.amazon.com/cloudfront/home?region=us-east-1#oai:)，或使用 CloudFront API 中的 [https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_ListCloudFrontOriginAccessIdentities.html](https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_ListCloudFrontOriginAccessIdentities.html)。
+ 用您的儲存貯體名稱取代 `amzn-s3-demo-bucket`。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Id": "PolicyForCloudFrontPrivateContent",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity EH1HDMB1FH2TC"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
        }
    ]
}
```

------

## 管理對 Amazon S3 Storage Lens 的存取
<a name="example-bucket-policies-lens"></a>

### 授予 Amazon S3 Storage Lens 的許可
<a name="example-bucket-policies-lens-1"></a>

S3 Storage Lens 會彙總您的指標，並在 Amazon S3 主控台 **Buckets** (儲存貯體) 頁面上的 **Account snapshot** (帳戶快照) 區段中顯示資訊。S3 Storage Lens 也提供互動式儀表板，您可以用來視覺化洞見和趨勢、標記極端值，以及接收最佳化儲存成本和套用資料保護最佳實務的建議。您的儀表板具有向下切入選項，可在組織、帳戶、 AWS 區域儲存類別、儲存貯體、字首或 Storage Lens 群組層級產生和視覺化洞見。您也可以將 CSV 或 Parquet 格式的每日指標報告傳送至一般用途 S3 儲存貯體，或直接將指標匯出至 AWS受管 S3 資料表儲存貯體。

S3 Storage Lens 可將您彙總的儲存體用量指標匯出到 Amazon S3 儲存貯體，以供進一步分析。S3 Storage Lens 存放其指標匯出的儲存貯體稱為「目的地儲存貯體」**。設定 S3 Storage Lens 指標匯出時，您必須具有目的地儲存貯體的儲存貯體政策。如需詳細資訊，請參閱[使用 Amazon S3 Storage Lens 監控您的儲存活動和用量](storage_lens.md)。

下列範例儲存貯體政策授予 Amazon S3 許可，將物件 (`PUT` 請求) 寫入至目的地儲存貯體。設定 S3 Storage Lens 指標匯出時，您可以對*目的地儲存貯體*使用類似這樣的儲存貯體政策。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "S3StorageLensExamplePolicy",
            "Effect": "Allow",
            "Principal": {
                "Service": "storage-lens.s3.amazonaws.com"
            },
            "Action": "s3:PutObject",
            "Resource": [
                "arn:aws:s3:::amzn-s3-demo-destination-bucket/destination-prefix/StorageLens/111122223333/*"
            ],
            "Condition": {
                "StringEquals": {
                    "s3:x-amz-acl": "bucket-owner-full-control",
                    "aws:SourceAccount": "111122223333",
                    "aws:SourceArn": "arn:aws:s3:region-code:111122223333:storage-lens/storage-lens-dashboard-configuration-id"
                }
            }
        }
    ]
}
```

------

當您設定 S3 Storage Lens 組織層級指標匯出時，對上一個儲存貯體政策的 `Resource` 陳述式使用下列修改。

```
1. "Resource": "arn:aws:s3:::amzn-s3-demo-destination-bucket/destination-prefix/StorageLens/your-organization-id/*",
```

## 管理 S3 庫存、S3 分析和 S3 庫存報告的許可
<a name="example-bucket-policies-s3-inventory"></a>

### 授予 S3 清查與 S3 分析的許可
<a name="example-bucket-policies-s3-inventory-1"></a>

S3 清查會建立儲存貯體中的物件清單，而 S3 分析儲存體類別分析匯出會建立分析中所使用資料的輸出檔案。庫存列出其物件的儲存貯體稱為*來源儲存貯體*。庫存檔案和分析匯出檔案撰寫至其中的儲存貯體稱為「目的地儲存貯體」**。設定庫存或分析匯出時，您必須針對目的地儲存貯體建立儲存貯體政策。如需詳細資訊，請參閱[使用 S3 庫存清單編目和分析資料](storage-inventory.md)及[Amazon S3 分析 – 儲存類別分析](analytics-storage-class.md)。

下列儲存貯體政策範例授予 Amazon S3 許可，允許從來源儲存貯體的帳戶將物件 (`PUT` 請求) 寫入至目的地儲存貯體。設定 S3 清查與 S3 分析匯出時，您可以在目標儲存貯體上使用這類的儲存貯體政策。

------
#### [ JSON ]

****  

```
{  
      "Version":"2012-10-17",		 	 	 
      "Statement": [
        {
            "Sid": "InventoryAndAnalyticsExamplePolicy",
            "Effect": "Allow",
            "Principal": {
                "Service": "s3.amazonaws.com"
            },
            "Action": "s3:PutObject",
            "Resource": [
            "arn:aws:s3:::amzn-s3-demo-destination-bucket/*"
            ],
            "Condition": {
                "ArnLike": {
                "aws:SourceArn": "arn:aws:s3:::amzn-s3-demo-source-bucket"
                },
                "StringEquals": {
                    "aws:SourceAccount": "111122223333",
                    "s3:x-amz-acl": "bucket-owner-full-control"
                }
            }
        }
    ]
}
```

------

### 控制 S3 庫存報告組態建立
<a name="example-bucket-policies-s3-inventory-2"></a>

[使用 S3 庫存清單編目和分析資料](storage-inventory.md) 會建立 S3 儲存貯體中物件的清單，以及各物件的中繼資料。`s3:PutInventoryConfiguration` 許可允許使用者建立預設包含所有物件中繼資料欄位的庫存組態，以及指定要儲存庫存的目的地儲存貯體。對目的地儲存貯體中的物件具有讀取權限的使用者可以存取庫存報告中所有可用的物件中繼資料欄位。如需 S3 庫存中可用的中繼資料欄位的詳細資訊，請參閱 [Amazon S3 清查清單](storage-inventory.md#storage-inventory-contents)。

若要限制使用者不得設定 S3 庫存報告，請移除該使用者的 `s3:PutInventoryConfiguration` 許可。

S3 庫存報告組態中的某些物件中繼資料欄位是選用的，這表示這些欄位預設可供使用，但當您授予使用者 `s3:PutInventoryConfiguration` 許可時，這些欄位可能會受到限制。您可以使用 `s3:InventoryAccessibleOptionalFields` 條件索引鍵，控制使用者是否可以在報告中包含這些選用的中繼資料欄位。如需 S3 庫存清單中可用的選用中繼資料欄位清單，請參閱 Amazon Simple Storage Service API 參考**中的[https://docs.aws.amazon.com//AmazonS3/latest/API/API_PutBucketInventoryConfiguration.html#API_PutBucketInventoryConfiguration_RequestBody](https://docs.aws.amazon.com//AmazonS3/latest/API/API_PutBucketInventoryConfiguration.html#API_PutBucketInventoryConfiguration_RequestBody)。

若要准許使用者建立具有特定選用中繼資料欄位的庫存組態，請使用 `s3:InventoryAccessibleOptionalFields` 條件索引鍵來縮小儲存貯體政策中套用的條件。

下列範例政策准許使用者 (`Ana`) 有條件地建立庫存組態。政策中的 `ForAllValues:StringEquals` 條件使用 `s3:InventoryAccessibleOptionalFields` 條件索引鍵來指定兩個允許的選用中繼資料欄位，即 `Size` 和 `StorageClass`。因此，當 `Ana` 建立庫存組態時，唯一可包含的選用中繼資料欄位是 `Size` 和 `StorageClass`。

------
#### [ JSON ]

****  

```
{
	"Id": "InventoryConfigPolicy",
	"Version":"2012-10-17",		 	 	 
	"Statement": [{
			"Sid": "AllowInventoryCreationConditionally",
			"Effect": "Allow",			
			"Principal": {
				"AWS": "arn:aws:iam::111122223333:user/Ana"
			},			
			"Action": 
				"s3:PutInventoryConfiguration",
			"Resource": 
				"arn:aws:s3:::DOC-EXAMPLE-SOURCE-BUCKET",
			"Condition": {
				"ForAllValues:StringEquals": {
					"s3:InventoryAccessibleOptionalFields": [
					   "Size",
					   "StorageClass"
					   ]
				  }
				}
			}
	]
}
```

------

若要限制使用者不得設定包含特定選用中繼資料欄位的 S3 庫存報告，請將明確的 `Deny` 陳述式新增至來源儲存貯體的儲存貯體政策。下列範例儲存貯體政策會拒絕使用者 `Ana` 在來源儲存貯體 `amzn-s3-demo-source-bucket` 中，建立包含選用 `ObjectAccessControlList` 或 `ObjectOwner` 中繼資料欄位的庫存組態。使用者 `Ana` 仍然可以建立具有其他選用中繼資料欄位的庫存組態。

```
 1. {
 2. 	"Id": "InventoryConfigSomeFields",
 3. 	"Version": "2012-10-17",		 	 	 
 4. 	"Statement": [{
 5. 			"Sid": "AllowInventoryCreation",
 6. 			"Effect": "Allow",
 7. 			"Principal": {
 8. 				"AWS": "arn:aws:iam::111122223333:user/Ana"
 9. 			},
10. 			"Action": "s3:PutInventoryConfiguration",			
11. 			"Resource": 
12. 				"arn:aws:s3:::amzn-s3-demo-source-bucket",
13. 
14. 		},
15. 		{
16. 			"Sid": "DenyCertainInventoryFieldCreation",
17. 			"Effect": "Deny",
18. 			"Principal": {
19. 				"AWS": "arn:aws:iam::111122223333:user/Ana"
20. 			},
21. 			"Action": "s3:PutInventoryConfiguration",	
22. 			"Resource": 
23. 			  "arn:aws:s3:::amzn-s3-demo-source-bucket",			
24. 			"Condition": {
25. 				"ForAnyValue:StringEquals": {
26. 					"s3:InventoryAccessibleOptionalFields": [
27. 					   "ObjectOwner",
28. 					   "ObjectAccessControlList"
29. 					   ]
30. 				  }
31. 				}
32. 			}
33. 	]
34. }
```

**注意**  
在儲存貯體政策中使用 `s3:InventoryAccessibleOptionalFields` 條件索引鍵不會對根據現有庫存組態傳送庫存報告造成影響。

**重要**  
建議您使用 `ForAllValues` 搭配 `Allow` 效果或使用 `ForAnyValue` 搭配 `Deny`效果，如先前範例所示。  
不要使用 `ForAllValues` 搭配 `Deny` 效果或使用 `ForAnyValue` 搭配 `Allow` 效果，因為這些組合可能會過度限制並封鎖庫存組態刪除。  
若要進一步了解 `ForAllValues` 和 `ForAnyValue` 條件集運算子，請參閱《IAM 使用者指南》**中的[多值內容金鑰](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-single-vs-multi-valued-context-keys.html#reference_policies_condition-multi-valued-context-keys)。

## 需要 MFA
<a name="example-bucket-policies-MFA"></a>

Amazon S3 支援 MFA 保護的 API 存取，在存取 Amazon S3 資源時，此功能可以強制執行 Multi-Factor Authentication (MFA)。多重要素驗證提供額外層級的安全性，您可以套用至您的 AWS 環境。MFA 是一種安全功能，需要使用者提供有效的 MFA 代碼來證明 MFA 裝置的實體擁有。如需詳細資訊，請參閱 [AWS 多重要素驗證](https://aws.amazon.com/mfa/)。只要是請求存取 Amazon S3 資源，您都可以要求 MFA。

若要強制執行 MFA 要求，請在儲存貯體政策中使用 `aws:MultiFactorAuthAge` 條件金鑰。IAM 使用者可以使用 AWS Security Token Service () 發行的臨時登入資料來存取 Amazon S3 資源AWS STS。您可以在 AWS STS 請求時提供 MFA 代碼。

當 Amazon S3 收到實施多重要素驗證的請求時，`aws:MultiFactorAuthAge` 條件金鑰會提供一個數值，指出暫時憑證是在多久之前建立 (以秒為單位)。如果要求中所提供的暫時性憑證不是使用 MFA 裝置所建立，則此金鑰值為 null (不存在)。在儲存貯體政策中，您可以新增可檢查此值的條件，如下列範例所示。

如果請求並未使用 MFA 進行驗證，此範例政策會拒絕對 `amzn-s3-demo-bucket` 儲存貯體中的 *`/taxdocuments`* 資料夾進行任何 Amazon S3 操作。若要進一步了解 MFA，請參閱*《IAM 使用者指南》*中的[在 AWS中使用多重要素驗證 (MFA)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa.html)。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Id": "123",
    "Statement": [
      {
        "Sid": "",
        "Effect": "Deny",
        "Principal": "*",
        "Action": "s3:*",
        "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/taxdocuments/*",
        "Condition": { "Null": { "aws:MultiFactorAuthAge": true }}
      }
    ]
 }
```

------

如果 `aws:MultiFactorAuthAge` 條件金鑰值為 null，則 `Condition` 區塊中的 `Null` 條件會評估為 `true`，表示已在沒有 MFA 裝置的情況下建立請求中的暫時性安全憑證。

下列儲存貯體政策是先前儲存貯體政策的延伸。下列政策包含兩個政策陳述式。一個陳述式會允許任何人在儲存貯體 (`amzn-s3-demo-bucket`) 上的 `s3:GetObject` 許可。另一個陳述式則會透過要求 MFA，進一步限制對儲存貯體中 `amzn-s3-demo-bucket/taxdocuments` 資料夾的存取。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Id": "123",
    "Statement": [
      {
        "Sid": "DenyInsecureConnections",
        "Effect": "Deny",
        "Principal": {
            "AWS": "arn:aws:iam::111122223333:root"
        },
        "Action": "s3:*",
        "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/taxdocuments/*",
        "Condition": { "Null": { "aws:MultiFactorAuthAge": true } }
      },
      {
        "Sid": "AllowGetObject",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::111122223333:root"
        },
        "Action": ["s3:GetObject"],
        "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
      }
    ]
 }
```

------

您可以選擇性地使用數值條件，來限制 `aws:MultiFactorAuthAge` 金鑰有效的持續時間。您使用 `aws:MultiFactorAuthAge` 金鑰指定的持續時間，與驗證請求時所使用之臨時安全憑證的生命週期無關。

例如，除了需要 MFA 身分驗證之外，下列儲存貯體政策也會檢查在多久之前建立暫時性工作階段。如果 `aws:MultiFactorAuthAge` 金鑰值指出已在一小時 (3,600 秒) 之前建立暫時性工作階段，則此政策會拒絕任何操作。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Id": "123",
    "Statement": [
      {
        "Sid": "",
        "Effect": "Deny",
        "Principal": "*",
        "Action": "s3:*",
        "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/taxdocuments/*",
        "Condition": {"Null": {"aws:MultiFactorAuthAge": true }}
      },
      {
        "Sid": "",
        "Effect": "Deny",
        "Principal": "*",
        "Action": "s3:*",
        "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/taxdocuments/*",
        "Condition": {"NumericGreaterThan": {"aws:MultiFactorAuthAge": 3600 }}
       },
       {
         "Sid": "",
         "Effect": "Allow",
         "Principal": "*",
         "Action": ["s3:GetObject"],
         "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
       }
    ]
 }
```

------

## 防止使用者刪除物件
<a name="using-with-s3-actions-related-to-bucket-subresources"></a>

這些使用者預設不具備任何許可。但當您建立政策時，您可能會授予使用者不打算授予的許可。為避免此等許可漏洞，您可以新增明確拒絕，撰寫較嚴格的存取政策。

若要明確禁止使用者或帳戶刪除物件，您必須將下列動作新增至儲存貯體政策：`s3:DeleteObject`、`s3:DeleteObjectVersion` 和 `s3:PutLifecycleConfiguration` 許可。由於您可以明確呼叫 `DELETE Object` API 操作來刪除物件，或設定其生命週期 (請參閱[管理物件的生命週期](object-lifecycle-mgmt.md))，讓 Amazon S3 在物件生命週期到期時移除物件，因此需要所有三個動作。

在下列政策範例中，您明確拒絕使用者 `MaryMajor` 的 `DELETE Object` 許可。明確的 `Deny` 陳述式一律會取代任何其他授予的許可。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "statement1",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:user/MaryMajor"
      },
      "Action": [
        "s3:GetObjectVersion",
        "s3:GetBucketAcl"
      ],
      "Resource": [
        "arn:aws:s3:::amzn-s3-demo-bucket1",
	 	"arn:aws:s3:::amzn-s3-demo-bucket1/*"
      ]
    },
    {
      "Sid": "statement2",
      "Effect": "Deny",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:user/MaryMajor"
      },
      "Action": [
        "s3:DeleteObject",
        "s3:DeleteObjectVersion",
        "s3:PutLifecycleConfiguration"
      ],
      "Resource": [
        "arn:aws:s3:::amzn-s3-demo-bucket1",
	    "arn:aws:s3:::amzn-s3-demo-bucket1/*"
      ]
    }
  ]
}
```

------