

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

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

# 使用 API Gateway 管理 API 的代码示例 AWS SDKs
<a name="apigatewaymanagementapi_code_examples"></a>

以下代码示例向您展示了如何将 Amazon API Gateway 管理 API 与 AWS 软件开发套件 (SDK) 一起使用。

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

**更多资源**
+  **[API Gateway 管理 API 开发人员指南](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html)**——有关 API Gateway 管理 API 的更多信息。
+ **[API Gateway 管理 API API 参考](https://docs.aws.amazon.com/apigateway/latest/api/API_Operations.html)**——有关所有可用的 API Gateway 管理 API 操作的详细信息。
+ **[AWS 开发者中心](https://aws.amazon.com/developer/code-examples/?awsf.sdk-code-examples-product=product%23api-gateway)** — 您可以按类别或全文搜索筛选的代码示例。
+ **[AWS SDK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)** — 包含首选语言完整代码的 GitHub 存储库。包括有关设置和运行代码的说明。

**Contents**
+ [基本功能](apigatewaymanagementapi_code_examples_basics.md)
  + [操作](apigatewaymanagementapi_code_examples_actions.md)
    + [`PostToConnection`](apigatewaymanagementapi_example_apigatewaymanagementapi_PostToConnection_section.md)

# 使用 API Gateway 管理 API 的基本示例 AWS SDKs
<a name="apigatewaymanagementapi_code_examples_basics"></a>

以下代码示例展示了如何使用 Amazon API Gateway 管理 API 的基础知识 AWS SDKs。

**Contents**
+ [操作](apigatewaymanagementapi_code_examples_actions.md)
  + [`PostToConnection`](apigatewaymanagementapi_example_apigatewaymanagementapi_PostToConnection_section.md)

# 使用 API Gateway 管理 API 的操作 AWS SDKs
<a name="apigatewaymanagementapi_code_examples_actions"></a>

以下代码示例演示了如何使用执行单个 API Gateway 管理 API 操作 AWS SDKs。每个示例都包含一个指向的链接 GitHub，您可以在其中找到有关设置和运行代码的说明。

 以下示例仅包括最常用的操作。有关完整列表，请参阅 [Amazon API Gateway Management API API 参考](https://docs.aws.amazon.com/apigateway/latest/api/API_Operations.html)。

**Topics**
+ [`PostToConnection`](apigatewaymanagementapi_example_apigatewaymanagementapi_PostToConnection_section.md)

# `PostToConnection`与 AWS SDK 或 CLI 配合使用
<a name="apigatewaymanagementapi_example_apigatewaymanagementapi_PostToConnection_section"></a>

以下代码示例演示如何使用 `PostToConnection`。

------
#### [ CLI ]

**AWS CLI**  
**向 WebSocket 连接发送数据**  
以下`post-to-connection`示例向连接到指定 WebSocket API 的客户端发送消息。  

```
aws apigatewaymanagementapi post-to-connection \
    --connection-id L0SM9cOFvHcCIhw= \
    --data "Hello from API Gateway!" \
    --endpoint-url https://aabbccddee.execute-api.us-west-2.amazonaws.com/prod
```
此命令不生成任何输出。  
有关更多信息，请参阅《Amazon API Gateway 开发人员指南》**中的[在后端服务中使用 @connections 命令](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[PostToConnection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/apigatewaymanagementapi/post-to-connection.html)*中的。

------
#### [ Rust ]

**适用于 Rust 的 SDK**  
 还有更多相关信息 GitHub。在 [AWS 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rustv1/examples/apigatewaymanagement#code-examples)中查找完整示例，了解如何进行设置和运行。

```
async fn send_data(
    client: &aws_sdk_apigatewaymanagement::Client,
    con_id: &str,
    data: &str,
) -> Result<(), aws_sdk_apigatewaymanagement::Error> {
    client
        .post_to_connection()
        .connection_id(con_id)
        .data(Blob::new(data))
        .send()
        .await?;

    Ok(())
}

    let endpoint_url = format!(
        "https://{api_id}.execute-api.{region}.amazonaws.com/{stage}",
        api_id = api_id,
        region = region,
        stage = stage
    );

    let shared_config = aws_config::from_env().region(region_provider).load().await;
    let api_management_config = config::Builder::from(&shared_config)
        .endpoint_url(endpoint_url)
        .build();
    let client = Client::from_conf(api_management_config);
```
+  有关 API 的详细信息，请参阅适用[PostToConnection](https://docs.rs/aws-sdk-apigatewaymanagementapi/latest/aws_sdk_apigatewaymanagementapi/client/struct.Client.html#method.post_to_connection)于 *Rust 的AWS SDK API 参考*。

------