DescribeEndpoint与 Amazon SDK 或 CLI 配合使用 - Amazon IoT Core
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

DescribeEndpoint与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 DescribeEndpoint

.NET
适用于 .NET 的 Amazon SDK (v4)
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Gets the AWS IoT endpoint URL. /// </summary> /// <returns>The endpoint URL, or null if retrieval failed.</returns> public async Task<string?> DescribeEndpointAsync() { try { var request = new DescribeEndpointRequest { EndpointType = "iot:Data-ATS" }; var response = await _amazonIoT.DescribeEndpointAsync(request); _logger.LogInformation($"Retrieved endpoint: {response.EndpointAddress}"); return response.EndpointAddress; } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return null; } catch (Exception ex) { _logger.LogError($"Couldn't describe endpoint. Here's why: {ex.Message}"); return null; } }
  • 有关 API 的详细信息,请参阅 适用于 .NET 的 Amazon SDK API 参考DescribeEndpoint中的。

C++
SDK for C++
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

//! Describe the endpoint specific to the AWS account making the call. /*! \param endpointResult: String to receive the endpoint result. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::describeEndpoint(Aws::String &endpointResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::String endpoint; Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DescribeEndpointRequest describeEndpointRequest; describeEndpointRequest.SetEndpointType( "iot:Data-ATS"); // Recommended endpoint type. Aws::IoT::Model::DescribeEndpointOutcome outcome = iotClient.DescribeEndpoint( describeEndpointRequest); if (outcome.IsSuccess()) { std::cout << "Successfully described endpoint." << std::endl; endpointResult = outcome.GetResult().GetEndpointAddress(); } else { std::cerr << "Error describing endpoint" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关 API 的详细信息,请参阅 适用于 C++ 的 Amazon SDK API 参考DescribeEndpoint中的。

CLI
Amazon CLI

示例 1:获取您当前的 Amazon 终端节点

以下describe-endpoint示例检索应用所有命令的默认 Amazon 端点。

aws iot describe-endpoint

输出:

{ "endpointAddress": "abc123defghijk.iot.us-west-2.amazonaws.com" }

有关更多信息,请参阅DescribeEndpointAmazon 物联网开发人员指南》

示例 2:获取您的 ATS 端点

以下 describe-endpoint 示例检索 Amazon Trust Services(ATS)端点。

aws iot describe-endpoint \ --endpoint-type iot:Data-ATS

输出:

{ "endpointAddress": "abc123defghijk-ats.iot.us-west-2.amazonaws.com" }

有关更多信息,请参阅《物联网开发者指南》中的 X.509 证书和物 AmazonAmazon 联网。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考DescribeEndpoint中的。

Java
适用于 Java 的 SDK 2.x
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/** * Describes the endpoint of the IoT service asynchronously. * * @return A CompletableFuture containing the full endpoint URL. * * This method initiates an asynchronous request to describe the endpoint of the IoT service. * If the request is successful, it prints and returns the full endpoint URL. * If an exception occurs, it prints the error message. */ public String describeEndpoint() { CompletableFuture<DescribeEndpointResponse> future = getAsyncClient().describeEndpoint(DescribeEndpointRequest.builder().endpointType("iot:Data-ATS").build()); final String[] result = {null}; future.whenComplete((endpointResponse, ex) -> { if (endpointResponse != null) { String endpointUrl = endpointResponse.endpointAddress(); String exString = getValue(endpointUrl); String fullEndpoint = "https://" + exString + "-ats.iot.us-east-1.amazonaws.com"; System.out.println("Full Endpoint URL: " + fullEndpoint); result[0] = fullEndpoint; } else { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + cause.getMessage()); } } }); future.join(); return result[0]; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考DescribeEndpoint中的。

Kotlin
适用于 Kotlin 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

suspend fun describeEndpoint(): String? { val request = DescribeEndpointRequest {} IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> val endpointResponse = iotClient.describeEndpoint(request) val endpointUrl: String? = endpointResponse.endpointAddress val exString: String = getValue(endpointUrl) val fullEndpoint = "https://$exString-ats.iot.us-east-1.amazonaws.com" println("Full endpoint URL: $fullEndpoint") return fullEndpoint } }
  • 有关 API 的详细信息,请参阅适用DescribeEndpoint于 K otlin 的Amazon SDK API 参考

Python
适用于 Python 的 SDK(Boto3)
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

class IoTWrapper: """Encapsulates AWS IoT actions.""" def __init__(self, iot_client, iot_data_client=None): """ :param iot_client: A Boto3 AWS IoT client. :param iot_data_client: A Boto3 AWS IoT Data Plane client. """ self.iot_client = iot_client self.iot_data_client = iot_data_client @classmethod def from_client(cls): iot_client = boto3.client("iot") iot_data_client = boto3.client("iot-data") return cls(iot_client, iot_data_client) def describe_endpoint(self, endpoint_type="iot:Data-ATS"): """ Gets the AWS IoT endpoint. :param endpoint_type: The endpoint type. :return: The endpoint. """ try: response = self.iot_client.describe_endpoint(endpointType=endpoint_type) logger.info("Retrieved endpoint %s.", response["endpointAddress"]) except ClientError as err: if err.response["Error"]["Code"] == "ThrottlingException": logger.error("Request throttled. Please try again later.") else: logger.error( "Couldn't describe endpoint. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response["endpointAddress"]
  • 有关 API 的详细信息,请参阅适用DescribeEndpointPython 的Amazon SDK (Boto3) API 参考

Rust
适用于 Rust 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

async fn show_address(client: &Client, endpoint_type: &str) -> Result<(), Error> { let resp = client .describe_endpoint() .endpoint_type(endpoint_type) .send() .await?; println!("Endpoint address: {}", resp.endpoint_address.unwrap()); println!(); Ok(()) }
  • 有关 API 的详细信息,请参阅适用DescribeEndpointRust 的Amazon SDK API 参考

有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅Amazon IoT 与 Amazon SDK 一起使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。