

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

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

# 使用 Amazon IVS 聊天示例 AWS CLI
<a name="cli_2_ivschat_code_examples"></a>

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

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

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

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

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

### `create-chat-token`
<a name="ivschat_CreateChatToken_cli_2_topic"></a>

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

**AWS CLI**  
**创建聊天令牌**  
以下`create-chat-token`示例创建了一个加密的聊天令牌，该令牌用于建立与房间的个人 WebSocket 连接。该令牌的有效期为一分钟，通过该令牌建立的连接（会话）在指定期间有效。  

```
aws ivschat create-chat-token \
    --roomIdentifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6", \
    --userId" "11231234" \
    --capabilities "SEND_MESSAGE", \
    --sessionDurationInMinutes" 30
```
输出：  

```
{
    "token": "ACEGmnoq#1rstu2...BDFH3vxwy!4hlm!#5",
    "sessionExpirationTime": "2022-03-16T04:44:09+00:00"
    "state": "CREATING",
    "tokenExpirationTime": "2022-03-16T03:45:09+00:00"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的[步骤 3：对聊天客户端进行身份验证和授权](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[CreateChatToken](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/create-chat-token.html)*中的。

### `create-logging-configuration`
<a name="ivschat_CreateLoggingConfiguration_cli_2_topic"></a>

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

**AWS CLI**  
**创建聊天 LoggingConfiguration 资源**  
以下`create-logging-configuration`示例创建了一个 LoggingConfiguration 资源，允许客户端存储和记录已发送的消息。  

```
aws ivschat create-logging-configuration \
    --destination-configuration s3={bucketName=demo-logging-bucket} \
    --name "test-logging-config" \
    --tags "key1=value1, key2=value2"
```
输出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ",
    "createTime": "2022-09-14T17:48:00.653000+00:00",
    "destinationConfiguration": {
        "s3": {
            "bucketName": "demo-logging-bucket"
        }
    },
    "id": "ABcdef34ghIJ",
    "name": "test-logging-config",
    "state": "ACTIVE",
    "tags": { "key1" : "value1", "key2" : "value2" },
    "updateTime": "2022-09-14T17:48:01.104000+00:00"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[CreateLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/create-logging-configuration.html)*中的。

### `create-room`
<a name="ivschat_CreateRoom_cli_2_topic"></a>

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

**AWS CLI**  
**创建聊天室**  
以下 `create-room` 示例创建一个新的聊天室。  

```
aws ivschat create-room \
    --name "test-room-1" \
    --logging-configuration-identifiers "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ" \
    --maximum-message-length 256 \
    --maximum-message-rate-per-second 5
```
输出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6",
    "id": "g1H2I3j4k5L6",
    "createTime": "2022-03-16T04:44:09+00:00",
    "loggingConfigurationIdentifiers": ["arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"],
    "maximumMessageLength": 256,
    "maximumMessageRatePerSecond": 5,
    "name": "test-room-1",
    "tags": {}
    "updateTime": "2022-03-16T07:22:09+00:00"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的[步骤 2：创建聊天室](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[CreateRoom](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/create-room.html)*中的。

### `delete-logging-configuration`
<a name="ivschat_DeleteLoggingConfiguration_cli_2_topic"></a>

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

**AWS CLI**  
**删除聊天 LoggingConfiguration 资源**  
以下`delete-logging-configuration`示例删除指定 ARN 的 LoggingConfiguration 资源。  

```
aws ivschat delete-logging-configuration \
    --identifier "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"
```
此命令不生成任何输出。  
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DeleteLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/delete-logging-configuration.html)*中的。

### `delete-message`
<a name="ivschat_DeleteMessage_cli_2_topic"></a>

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

**AWS CLI**  
**从指定聊天室删除消息**  
以下 `delete-message` 示例发送一个事件给指定聊天室，指示客户端删除指定的消息，也就是说，取消呈现该消息，并将其从客户端的聊天记录中删除。  

```
aws ivschat delete-message \
    --roomIdentifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6" \
    --id "ABC123def456" \
    --reason "Message contains profanity"
```
输出：  

```
{
    "id": "12345689012"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DeleteMessage](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/delete-message.html)*中的。

### `delete-room`
<a name="ivschat_DeleteRoom_cli_2_topic"></a>

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

**AWS CLI**  
**删除聊天室**  
以下 `delete-room` 示例删除指定聊天室。连接的客户端断开连接。成功后，它将返回带有空响应正文的 HTTP 204。  

```
aws ivschat delete-room \
    --identifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6"
```
此命令不生成任何输出。  
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DeleteRoom](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/delete-room.html)*中的。

### `disconnect-user`
<a name="ivschat_DisconnectUser_cli_2_topic"></a>

以下代码示例演示了如何使用 `disconnect-user`。

**AWS CLI**  
**断开用户与聊天室的连接**  
以下 `disconnect-user` 示例断开指定用户与指定聊天室的所有连接。成功后，它将返回带有空响应正文的 HTTP 200。  

```
aws ivschat disconnect-user \
    --roomIdentifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6" \
    --userId "ABC123def456" \
    --reason "Violated terms of service"
```
此命令不生成任何输出。  
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DisconnectUser](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/disconnect-user.html)*中的。

### `get-logging-configuration`
<a name="ivschat_GetLoggingConfiguration_cli_2_topic"></a>

以下代码示例演示了如何使用 `get-logging-configuration`。

**AWS CLI**  
**获取有关 LoggingConfiguration 资源的信息**  
以下`get-logging-configuration`示例获取有关指定 ARN 的 LoggingConfiguration 资源的信息。  

```
aws ivschat get-logging-configuration \
    --identifier "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"
```
输出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ",
    "createTime": "2022-09-14T17:48:00.653000+00:00",
    "destinationConfiguration": {
        "s3": {
            "bucketName": "demo-logging-bucket"
        }
    },
    "id": "ABcdef34ghIJ",
    "name": "test-logging-config",
    "state": "ACTIVE",
    "tags": { "key1" : "value1", "key2" : "value2" },
    "updateTime": "2022-09-14T17:48:01.104000+00:00"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/get-logging-configuration.html)*中的。

### `get-room`
<a name="ivschat_GetRoom_cli_2_topic"></a>

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

**AWS CLI**  
**获取指定聊天室**  
以下 `get-room` 示例获取有关指定聊天室的信息。  

```
aws ivschat get-room \
    --identifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6"
```
输出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6",
    "createTime": "2022-03-16T04:44:09+00:00",
    "id": "g1H2I3j4k5L6",
    "loggingConfigurationIdentifiers": ["arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"],
    "maximumMessageLength": 256,
    "maximumMessageRatePerSecond": 5,
    "name": "test-room-1",
    "tags": {},
    "updateTime": "2022-03-16T07:22:09+00:00"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetRoom](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/get-room.html)*中的。

### `list-logging-configurations`
<a name="ivschat_ListLoggingConfigurations_cli_2_topic"></a>

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

**AWS CLI**  
**获取有关处理 API 请求的 AWS 区域中用户的所有日志配置的摘要信息**  
以下`list-logging-configurations`示例列出了处理 API 请求的 AWS 区域中该用户的所有 LoggingConfiguration 资源的信息。  

```
aws ivschat list-logging-configurations \
    --max-results 2 \
    --next-token ""
```
输出：  

```
{
    "nextToken": "set-2",
    "loggingConfigurations": [
        {
            "arn": "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ",
            "createTime": "2022-09-14T17:48:00.653000+00:00",
            "destinationConfiguration": {
                "s3": {
                    "bucketName": "demo-logging-bucket"
                }
            },
            "id": "ABcdef34ghIJ",
            "name": "test-logging-config",
            "state": "ACTIVE",
            "tags": { "key1" : "value1", "key2" : "value2" },
            "updateTime": "2022-09-14T17:48:01.104000+00:00"
        }
        ...
    ]
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[ListLoggingConfigurations](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/list-logging-configurations.html)*中的。

### `list-rooms`
<a name="ivschat_ListRooms_cli_2_topic"></a>

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

**AWS CLI**  
**获取当前区域内有关您的所有聊天室的摘要信息**  
以下`list-rooms`示例获取有关处理请求的 AWS 区域中所有房间的摘要信息。结果按 updateTime 降序排序。  

```
aws ivschat list-rooms \
    --logging-configuration-identifier "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ" \
    --max-results 10 \
    --next-token ""
```
输出：  

```
{
    "nextToken": "page3",
    "rooms": [
        {
            "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6",
            "createTime": "2022-03-16T04:44:09+00:00",
            "id": "g1H2I3j4k5L6",
            "loggingConfigurationIdentifiers": ["arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"],
            "name": "test-room-1",
            "tags": {},
            "updateTime": "2022-03-16T07:22:09+00:00"
        }
    ]
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[ListRooms](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/list-rooms.html)*中的。

### `list-tags-for-resource`
<a name="ivschat_ListTagsForResource_cli_2_topic"></a>

以下代码示例演示了如何使用 `list-tags-for-resource`。

**AWS CLI**  
**列出 AWS 资源的所有标签（例如：房间）**  
以下 `list-tags-for-resource` 示例列出指定资源 ARN（Amazon 资源名称）的所有标签。  

```
aws ivschat list-tags-for-resource \
    --resource-arn arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6
```
输出：  

```
{
    "tags":
    {
        "key1": "value1",
        "key2": "value2"
    }
}
```
有关更多信息，请参阅《Amazon Interactive Video Service API 参考》**中的[标记](https://docs.aws.amazon.com/ivs/latest/APIReference/Welcome.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/list-tags-for-resource.html)*中的。

### `send-event`
<a name="ivschat_SendEvent_cli_2_topic"></a>

以下代码示例演示了如何使用 `send-event`。

**AWS CLI**  
**将事件发送到聊天室**  
以下 `send-event` 示例将给定事件发送到指定的聊天室。  

```
aws ivschat send-event \
    --roomIdentifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6" \
    --eventName "SystemMessage" \
    --attributes \
        "msgType"="user-notification", \
        "msgText"="This chat room will close in 15 minutes."
```
输出：  

```
{
    "id": "12345689012"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[SendEvent](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/send-event.html)*中的。

### `tag-resource`
<a name="ivschat_TagResource_cli_2_topic"></a>

以下代码示例演示了如何使用 `tag-resource`。

**AWS CLI**  
**为 AWS 资源添加或更新标签（例如：房间）**  
以下 `tag-resource` 示例为指定资源 ARN（Amazon 资源名称）添加或更新标签。成功后，它将返回带有空响应正文的 HTTP 200。  

```
aws ivschat tag-resource \
    --resource-arn arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6 \
    --tags "tagkey1=tagkeyvalue1, tagkey2=tagkeyvalue2"
```
此命令不生成任何输出。  
有关更多信息，请参阅《Amazon Interactive Video Service API 参考》**中的[标记](https://docs.aws.amazon.com/ivs/latest/APIReference/Welcome.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/tag-resource.html)*中的。

### `untag-resource`
<a name="ivschat_UntagResource_cli_2_topic"></a>

以下代码示例演示了如何使用 `untag-resource`。

**AWS CLI**  
**移除 AWS 资源的标签（例如：房间）**  
以下 `untag-resource` 示例移除指定资源 ARN（Amazon 资源名称）的指定标签。成功后，它将返回带有空响应正文的 HTTP 200。  

```
aws ivschat untag-resource \
    --resource-arn arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6 \
    --tag-keys "tagkey1, tagkey2"
```
此命令不生成任何输出。  
有关更多信息，请参阅《Amazon Interactive Video Service API 参考》**中的[标记](https://docs.aws.amazon.com/ivs/latest/APIReference/Welcome.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/untag-resource.html)*中的。

### `update-logging-configuration`
<a name="ivschat_UpdateLoggingConfiguration_cli_2_topic"></a>

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

**AWS CLI**  
**更新聊天室的日志配置**  
以下`update-logging-configuration`示例使用给定数据更新 LoggingConfiguration 资源。  

```
aws ivschat update-logging-configuration \
    --destination-configuration s3={bucketName=demo-logging-bucket} \
    --identifier "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ" \
    --name "test-logging-config"
```
输出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ",
    "createTime": "2022-09-14T17:48:00.653000+00:00",
    "destinationConfiguration": {
        "s3": {
            "bucketName": "demo-logging-bucket"
        }
    },
    "id": "ABcdef34ghIJ",
    "name": "test-logging-config",
    "state": "ACTIVE",
    "tags": { "key1" : "value1", "key2" : "value2" },
    "updateTime": "2022-09-14T17:48:01.104000+00:00"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[UpdateLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/update-logging-configuration.html)*中的。

### `update-room`
<a name="ivschat_UpdateRoom_cli_2_topic"></a>

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

**AWS CLI**  
**更新聊天室配置**  
以下 `update-room` 示例使用给定数据更新指定聊天室的配置。  

```
aws ivschat update-room \
    --identifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6" \
    --logging-configuration-identifiers "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ" \
    --name "chat-room-a" \
    --maximum-message-length 256 \
    --maximum-message-rate-per-second 5
```
输出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6",
    "createTime": "2022-03-16T04:44:09+00:00",
    "id": "g1H2I3j4k5L6",
    "loggingConfigurationIdentifiers": ["arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"],
    "maximumMessageLength": 256,
    "maximumMessageRatePerSecond": 5,
    "name": "chat-room-a",
    "tags": {},
    "updateTime": "2022-03-16T07:22:09+00:00"
}
```
有关更多信息，请参阅《Amazon Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[UpdateRoom](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/update-room.html)*中的。