与 AWS SDK或AttachThingPrincipal一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或AttachThingPrincipal一起使用 CLI

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

C++
SDK对于 C++
注意

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

//! Attach a principal to an AWS IoT thing. /*! \param principal: A principal to attach. \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::attachThingPrincipal(const Aws::String &principal, const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient client(clientConfiguration); Aws::IoT::Model::AttachThingPrincipalRequest request; request.SetPrincipal(principal); request.SetThingName(thingName); Aws::IoT::Model::AttachThingPrincipalOutcome outcome = client.AttachThingPrincipal( request); if (outcome.IsSuccess()) { std::cout << "Successfully attached principal to thing." << std::endl; } else { std::cerr << "Failed to attach principal to thing." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
CLI
AWS CLI

在你的东西上附加证书

以下attach-thing-principal示例将证书附加到 MyTemperatureSensor 事物。该证书由标识ARN。您可以在 ARN AWS IoT 控制台中找到证书的。

aws iot attach-thing-principal \ --thing-name MyTemperatureSensor \ --principal arn:aws:iot:us-west-2:123456789012:cert/2e1eb273792174ec2b9bf4e9b37e6c6c692345499506002a35159767055278e8

此命令不生成任何输出。

有关更多信息,请参阅《AWS IoT 开发人员指南》中的如何使用注册表管理事物

Java
SDK适用于 Java 2.x
注意

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

/** * Attaches a certificate to an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing. * @param certificateArn The ARN of the certificate to attach. * * This method initiates an asynchronous request to attach a certificate to an IoT Thing. * If the request is successful, it prints a confirmation message and additional information about the Thing. * If an exception occurs, it prints the error message. */ public void attachCertificateToThing(String thingName, String certificateArn) { AttachThingPrincipalRequest principalRequest = AttachThingPrincipalRequest.builder() .thingName(thingName) .principal(certificateArn) .build(); CompletableFuture<AttachThingPrincipalResponse> future = getAsyncClient().attachThingPrincipal(principalRequest); future.whenComplete((attachResponse, ex) -> { if (attachResponse != null && attachResponse.sdkHttpResponse().isSuccessful()) { System.out.println("Certificate attached to Thing successfully."); // Print additional information about the Thing. describeThing(thingName); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to attach certificate to Thing. HTTP Status Code: " + attachResponse.sdkHttpResponse().statusCode()); } } }); future.join(); }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 AttachThingPrincipal” 中的。

Kotlin
SDK对于 Kotlin 来说
注意

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

suspend fun attachCertificateToThing( thingNameVal: String?, certificateArn: String?, ) { val principalRequest = AttachThingPrincipalRequest { thingName = thingNameVal principal = certificateArn } IotClient { region = "us-east-1" }.use { iotClient -> iotClient.attachThingPrincipal(principalRequest) println("Certificate attached to $thingNameVal successfully.") } }