文件範例儲存庫中有更多 AWS SDK可用的AWS SDK範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AttachThingPrincipal
搭配 AWS SDK或 使用 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(); }
-
如需API詳細資訊,請參閱 AWS SDK for C++ API 參考AttachThingPrincipal中的 。
-
- CLI
-
- AWS CLI
-
將憑證連接至您的物件
下列
attach-thing-principal
範例會將憑證連接至 MyTemperatureSensor 物件。憑證由 識別ARN。您可以在 AWS IoT 主控台中找到憑證ARN的 。aws iot attach-thing-principal \ --thing-name
MyTemperatureSensor
\ --principalarn:aws:iot:us-west-2:123456789012:cert/2e1eb273792174ec2b9bf4e9b37e6c6c692345499506002a35159767055278e8
此命令不會產生輸出。
如需詳細資訊,請參閱 AWS IoT 開發人員指南中的如何使用登錄檔管理物件。
-
如需API詳細資訊,請參閱 AWS CLI 命令參考AttachThingPrincipal
中的 。
-
- 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.") } }
-
如需API詳細資訊,請參閱 AttachThingPrincipal
中的 AWS SDK Kotlin API參考。
-
動作
CreateKeysAndCertificate