

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

# 單值與多值的內容索引鍵
<a name="reference_policies_condition-single-vs-multi-valued-context-keys"></a>

單值與多值內容索引鍵之間的差異在於[請求內容](intro-structure.md#intro-structure-request)中值的數目，而非政策條件中值的數目。
+ *單值*條件內容索引鍵在請求內容中最多有一個值。例如，當您在 中標記資源時 AWS，每個資源標籤都會儲存為索引鍵/值對。由於資源標籤金鑰只能有單一標籤值，因此 [aws:ResourceTag/*tag-key*](reference_policies_condition-keys.md#condition-keys-resourcetag) 是單值內容索引鍵。請勿使用具有單值內容索引鍵的條件集運算子。
+ *多值*條件內容索引鍵在請求內容中可以擁有多個值。例如，當您在 中標記資源時 AWS，您可以在單一請求中包含多個標籤鍵/值對。因此，[aws:TagKeys](reference_policies_condition-keys.md#condition-keys-tagkeys) 是多值內容索引鍵。多值內容索引鍵需要條件集運算子。

例如，請求可以來自最多一個 VPC 端點，因此 [aws:SourceVpce](reference_policies_condition-keys.md#condition-keys-sourcevpce) 是單值內容索引鍵。由於服務可以有多個屬於該服務的服務主體名稱，因此 [aws:PrincipalServiceNamesList](reference_policies_condition-keys.md#condition-keys-principalservicenameslist) 是多值內容索引鍵。

**重要**  
單值與多值內容索引鍵之間的差異取決於請求內容中值的數目，而非政策條件中值的數目。

## 重點
<a name="reference_policies_condition-key-points"></a>
+ *單值*和*多值*分類包含在 [AWS 全域條件內容索引鍵](reference_policies_condition-keys.md) 主題中作為*值類型*的每個條件內容索引鍵的描述中。
+ [Service Authorization Reference](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html) 中的多值內容索引鍵使用 `ArrayOf` 字首，後接條件運算子類別類型 (例如 `ArrayOfString` 或 `ArrayOfARN`)，表示請求可能包含條件內容索引鍵的多個值。
+ 可以使用任何可用的單值內容索引鍵作為政策變數，但不能使用多值內容金鑰作為政策變數。如需有關政策變數的詳細資訊，請參閱 [IAM 政策元素：變數與標籤](reference_policies_variables.md)。
+ 使用包含金鑰/值對的內容索引鍵時，請務必注意，即使可以有多個標籤鍵值，每個 `tag-key` 只能有一個值。
  + [aws:PrincipalTag/*tag-key*](reference_policies_condition-keys.md#condition-keys-principaltag)、[aws:RequestTag/*tag-key*](reference_policies_condition-keys.md#condition-keys-requesttag) 和 [aws:ResourceTag/*tag-key*](reference_policies_condition-keys.md#condition-keys-resourcetag) 是單值內容索引鍵。
  + [aws:TagKeys](reference_policies_condition-keys.md#condition-keys-tagkeys) 定義了請求中允許的標籤索引鍵，但不包含標籤索引鍵值。由於您可以在請求中包含多個標籤索引鍵值對，因此 `aws:TagKeys` 是多值內容索引鍵。
+ 多值內容索引鍵需要條件集運算子。請勿將條件集運算子 `ForAllValues` 或 `ForAnyValue` 與單值內容索引鍵搭配使用。使用條件集運算子搭配單值內容索引鍵可能會導致過度寬鬆的政策。

## 用於多值內容索引鍵的集運算子
<a name="reference_policies_condition-multi-valued-context-keys"></a>

若要將您的條件內容索引鍵與具有多值的[請求內容](intro-structure.md#intro-structure-request)索引鍵進行比較，您必須使用 `ForAllValues` 或 `ForAnyValue` 集運算子。這些集運算子用於比較兩組值，例如請求中的標籤集和政策條件中的標籤集。

`ForAllValues` 和 `ForAnyValue` 限定詞將集操作功能新增到了條件運算子，允許您針對政策條件中的多內容索引鍵值來測試具有多值的請求內容索引鍵。此外，如果您在政策中使用萬用字元或變數包含多值字串內容索引鍵，您也必須使用 `StringLike` [條件運算子](reference_policies_elements_condition_operators.md#Conditions_String)。多個條件索引鍵值必須像[陣列](reference_policies_grammar.md#policies-grammar-json)那樣用方括號括住，例如 `"Key2":["Value2A", "Value2B"]`。

### ForAllValues
<a name="reference_policies_condition-forallvalues"></a>

`ForAllValues` 限定詞會測試請求內容中每個成員的值是否與限定詞後面的條件運算子相符。如果請求中每個內容索引鍵值都與政策中的內容索引鍵值相符，則條件會傳回 `true`。如果請求中沒有內容索引鍵，也會傳回 `true`。

**重要**  
如果將 `ForAllValues` 與 `Allow` 效果搭配使用，請務必小心，因為如果請求內容中意外出現遺失內容索引鍵，則可能導致政策過度寬鬆。您應該始終在政策中包含具有 `false` 值的 [`Null`](reference_policies_elements_condition_operators.md#Conditions_Null) 條件運算子，以檢查內容索引鍵是否存在且其值不為 Null。如需範例，請參閱 [根據標籤索引鍵控制存取權限](access_tags.md#access_tags_control-tag-keys)。

#### ForAllValues 集運算子範例
<a name="reference_policies_condition-forallvalues-example"></a>

在下列範例中，ForAllValues 會與 aws:TagKeys 搭配使用，以允許使用者刪除指派給 EC2 執行個體的特定標籤。此政策僅允許使用者刪除 `environment` 和 `cost-center` 標籤。您可以將其單獨刪除，也可以同時刪除。請求中的標籤索引鍵必須與政策中指定的索引鍵完全相符。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:DeleteTags",
            "Resource": "arn:aws:ec2:us-east-1:{{111122223333}}:instance/*",
            "Condition": {
                "ForAllValues:StringEquals": {
                    "aws:TagKeys": [
                        "environment",
                        "cost-center"
                    ]
                },
                "Null": {
                    "aws:TagKeys": "false"
                }
            }
        }
    ]
}
```

------

下表顯示 如何根據請求中的條件索引鍵值來 AWS 評估此政策。


| 政策條件 | 請求內容 | 結果 | 
| --- | --- | --- | 
|  <pre>"ForAllValues:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />},<br />"Null": {<br />  "aws:TagKeys": "false"<br />}</pre>  | <pre>aws:TagKeys:<br />  – environment</pre>  |  **相符**  | 
|  <pre>"ForAllValues:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />},<br />"Null": {<br />  "aws:TagKeys": "false"<br />}</pre>  | <pre>aws:TagKeys:<br />  – cost-center</pre>  |  **相符**  | 
|  <pre>"ForAllValues:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />},<br />"Null": {<br />  "aws:TagKeys": "false"<br />}</pre>  | <pre>aws:TagKeys:<br />  – environment<br />  – cost-center</pre>  |  **相符**  | 
|  <pre>"ForAllValues:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />},<br />"Null": {<br />  "aws:TagKeys": "false"<br />}</pre>  | <pre>aws:TagKeys:<br />  – environment<br />  – dept</pre>  |  **沒有相符項目**  | 
|  <pre>"ForAllValues:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />},<br />"Null": {<br />  "aws:TagKeys": "false"<br />}</pre>  |  請求內容中沒有 `aws:TagKeys`。  |  **沒有相符項目**  | 

請注意，在最後一個範例中，結果為「無相符項目」，因為 Null 條件檢查會在遺失內容索引鍵時防止相符。這是避免政策過度寬鬆的最佳實務。

### ForAnyValue
<a name="reference_policies_condition-foranyvalue"></a>

`ForAnyValue` 限定詞會測試這組請求內容索引鍵值是否至少有一個成員與政策條件中這組內容索引鍵值的至少一個成員相符。如果請求中任一內容索引鍵值與政策中的任一內容索引鍵值相符，則條件會傳回 `true`。如果沒有相符的內容索引鍵，或者如果索引鍵不存在，則條件會傳回 `false`。

**重要**  
將 `ForAnyValue` 與 `Deny` 效果搭配使用時，如果請求中不存在內容索引鍵，政策會評估為**無相符項目**。為實現一致的行為，請在政策中新增明確的 [`Null`](reference_policies_elements_condition_operators.md#Conditions_Null) 條件檢查，以驗證內容索引鍵是否存在。如需詳細資訊，請參閱[用於檢查條件索引鍵是否存在的條件運算子](reference_policies_elements_condition_operators.md#Conditions_Null)。

#### ForAnyValue 集運算子範例
<a name="reference_policies_condition-foranyvalue-example"></a>

在下列範例中，ForAnyValue 會與 aws:TagKeys 搭配使用，以允許使用者刪除指派給 EC2 執行個體的特定標籤。如果請求中指定的標籤索引鍵包含 `environment` 或 `cost-center`，則此政策允許使用者刪除執行個體的標籤。請求可以包含政策中指定標籤索引鍵之外的其他標籤索引鍵，但必須包含至少一個指定索引鍵以符合條件。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:DeleteTags",
            "Resource": "arn:aws:ec2:us-east-1:{{111122223333}}:instance/*",
            "Condition": {
                "ForAnyValue:StringEquals": {
                    "aws:TagKeys": [
                        "environment",
                        "cost-center"
                    ]
                }
            }
        }
    ]
}
```

------

下表顯示 如何根據請求中的條件索引鍵值來 AWS 評估此政策。


| 政策條件 | 請求內容 | 結果 | 
| --- | --- | --- | 
|  <pre>"ForAnyValue:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />}</pre>  | <pre>aws:TagKeys:<br />  – environment</pre>  |  **相符**  | 
|  <pre>"ForAnyValue:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />}</pre>  | <pre>aws:TagKeys:<br />  – cost-center</pre>  |  **相符**  | 
|  <pre>"ForAnyValue:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />}</pre>  | <pre>aws:TagKeys:<br />  – environment<br />  – cost-center</pre>  |  **相符**  | 
|  <pre>"ForAnyValue:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />}</pre>  | <pre>aws:TagKeys:<br />  – environment<br />  – dept</pre>  |  **相符**  | 
|  <pre>"ForAnyValue:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />}</pre>  | <pre>aws:TagKeys:<br />  – dept</pre>  |  **沒有相符項目**  | 
|  <pre>"ForAnyValue:StringEquals": {<br />  "aws:TagKeys": [<br />    "environment",<br />    "cost-center"<br />  ]<br />}</pre>  |  請求內容中沒有 `aws:TagKeys`。  |  **沒有相符項目**  | 