

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Amazon Cognito 身份示例 AWS CLI
<a name="cli_2_cognito-identity_code_examples"></a>

以下代码示例向您展示了如何使用 AWS Command Line Interface 与 Amazon Cognito Identity 配合使用来执行操作和实现常见场景。

*操作*是大型程序的代码摘录，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**Topics**
+ [操作](#actions)

## 操作
<a name="actions"></a>

### `create-identity-pool`
<a name="cognito-identity_CreateIdentityPool_cli_2_topic"></a>

以下代码示例演示了如何使用 `create-identity-pool`。

**AWS CLI**  
**创建带有 Cognito 身份池提供者的身份池**  
此示例创建了一个名为的身份池 MyIdentityPool。它有一个 Cognito 身份池提供者。不允许使用未经身份验证的身份。  
命令:  

```
aws cognito-identity create-identity-pool --identity-pool-name MyIdentityPool --no-allow-unauthenticated-identities --cognito-identity-providers ProviderName="cognito-idp.us-west-2.amazonaws.com/us-west-2_aaaaaaaaa",ClientId="3n4b5urk1ft4fl3mg5e62d9ado",ServerSideTokenCheck=false
```
输出：  

```
{
  "IdentityPoolId": "us-west-2:11111111-1111-1111-1111-111111111111",
  "IdentityPoolName": "MyIdentityPool",
  "AllowUnauthenticatedIdentities": false,
  "CognitoIdentityProviders": [
      {
          "ProviderName": "cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111",
          "ClientId": "3n4b5urk1ft4fl3mg5e62d9ado",
          "ServerSideTokenCheck": false
      }
  ]
}
```
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[CreateIdentityPool](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/create-identity-pool.html)*中的。

### `delete-identities`
<a name="cognito-identity_DeleteIdentities_cli_2_topic"></a>

以下代码示例演示了如何使用 `delete-identities`。

**AWS CLI**  
**删除身份池**  
此示例删除身份池。  
命令:  

```
aws cognito-identity delete-identity-pool --identity-ids-to-delete "us-west-2:11111111-1111-1111-1111-111111111111"
```
输出：  

```
{
  "UnprocessedIdentityIds": []
}
```
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DeleteIdentities](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/delete-identities.html)*中的。

### `delete-identity-pool`
<a name="cognito-identity_DeleteIdentityPool_cli_2_topic"></a>

以下代码示例演示了如何使用 `delete-identity-pool`。

**AWS CLI**  
**删除身份池**  
以下 `delete-identity-pool` 示例删除指定身份池。  
命令:  

```
aws cognito-identity delete-identity-pool \
    --identity-pool-id "us-west-2:11111111-1111-1111-1111-111111111111"
```
此命令不生成任何输出。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DeleteIdentityPool](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/delete-identity-pool.html)*中的。

### `describe-identity-pool`
<a name="cognito-identity_DescribeIdentityPool_cli_2_topic"></a>

以下代码示例演示了如何使用 `describe-identity-pool`。

**AWS CLI**  
**描述身份池**  
此示例描述身份池。  
命令:  

```
aws cognito-identity describe-identity-pool --identity-pool-id "us-west-2:11111111-1111-1111-1111-111111111111"
```
输出：  

```
{
  "IdentityPoolId": "us-west-2:11111111-1111-1111-1111-111111111111",
  "IdentityPoolName": "MyIdentityPool",
  "AllowUnauthenticatedIdentities": false,
  "CognitoIdentityProviders": [
      {
          "ProviderName": "cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111",
          "ClientId": "3n4b5urk1ft4fl3mg5e62d9ado",
          "ServerSideTokenCheck": false
      }
  ]
}
```
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DescribeIdentityPool](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/describe-identity-pool.html)*中的。

### `get-identity-pool-roles`
<a name="cognito-identity_GetIdentityPoolRoles_cli_2_topic"></a>

以下代码示例演示了如何使用 `get-identity-pool-roles`。

**AWS CLI**  
**获取身份池角色**  
此示例获取身份池角色。  
命令:  

```
aws cognito-identity get-identity-pool-roles --identity-pool-id "us-west-2:11111111-1111-1111-1111-111111111111"
```
输出：  

```
{
  "IdentityPoolId": "us-west-2:11111111-1111-1111-1111-111111111111",
  "Roles": {
      "authenticated": "arn:aws:iam::111111111111:role/Cognito_MyIdentityPoolAuth_Role",
      "unauthenticated": "arn:aws:iam::111111111111:role/Cognito_MyIdentityPoolUnauth_Role"
  }
}
```
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetIdentityPoolRoles](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/get-identity-pool-roles.html)*中的。

### `list-identity-pools`
<a name="cognito-identity_ListIdentityPools_cli_2_topic"></a>

以下代码示例演示了如何使用 `list-identity-pools`。

**AWS CLI**  
**列出身份池**  
此示例列出身份池。最多可列出 20 个身份。  
命令:  

```
aws cognito-identity list-identity-pools --max-results 20
```
输出：  

```
{
  "IdentityPools": [
      {
          "IdentityPoolId": "us-west-2:11111111-1111-1111-1111-111111111111",
          "IdentityPoolName": "MyIdentityPool"
      },
      {
          "IdentityPoolId": "us-west-2:11111111-1111-1111-1111-111111111111",
          "IdentityPoolName": "AnotherIdentityPool"
      },
      {
          "IdentityPoolId": "us-west-2:11111111-1111-1111-1111-111111111111",
          "IdentityPoolName": "IdentityPoolRegionA"
      }
  ]
}
```
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[ListIdentityPools](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/list-identity-pools.html)*中的。

### `set-identity-pool-roles`
<a name="cognito-identity_SetIdentityPoolRoles_cli_2_topic"></a>

以下代码示例演示了如何使用 `set-identity-pool-roles`。

**AWS CLI**  
**设置身份池角色**  
以下 `set-identity-pool-roles` 示例设置身份池角色。  

```
aws cognito-identity set-identity-pool-roles \
    --identity-pool-id "us-west-2:11111111-1111-1111-1111-111111111111" \
    --roles authenticated="arn:aws:iam::111111111111:role/Cognito_MyIdentityPoolAuth_Role"
```
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[SetIdentityPoolRoles](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/set-identity-pool-roles.html)*中的。

### `update-identity-pool`
<a name="cognito-identity_UpdateIdentityPool_cli_2_topic"></a>

以下代码示例演示了如何使用 `update-identity-pool`。

**AWS CLI**  
**更新身份池**  
此示例更新身份池。它将名称设置为 MyIdentityPool。它添加 Cognito 作为身份池提供者。它不允许使用未经身份验证的身份。  
命令:  

```
aws cognito-identity update-identity-pool --identity-pool-id "us-west-2:11111111-1111-1111-1111-111111111111" --identity-pool-name "MyIdentityPool" --no-allow-unauthenticated-identities --cognito-identity-providers ProviderName="cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111",ClientId="3n4b5urk1ft4fl3mg5e62d9ado",ServerSideTokenCheck=false
```
输出：  

```
{
  "IdentityPoolId": "us-west-2:11111111-1111-1111-1111-111111111111",
  "IdentityPoolName": "MyIdentityPool",
  "AllowUnauthenticatedIdentities": false,
  "CognitoIdentityProviders": [
      {
          "ProviderName": "cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111",
          "ClientId": "3n4b5urk1ft4fl3mg5e62d9ado",
          "ServerSideTokenCheck": false
      }
  ]
}
```
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[UpdateIdentityPool](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/update-identity-pool.html)*中的。