Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Verwendung AttachThingPrincipal mit einem AWS SDK oder CLI
Die folgenden Code-Beispiele zeigen, wie AttachThingPrincipal verwendet wird.
- .NET
-
- SDK für .NET(v4)
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS-Code-Beispiel-
einrichten und ausführen. /// <summary> /// Attaches a certificate to an IoT Thing. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <param name="certificateArn">The ARN of the certificate to attach.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> AttachThingPrincipalAsync(string thingName, string certificateArn) { try { var request = new AttachThingPrincipalRequest { ThingName = thingName, Principal = certificateArn }; await _amazonIoT.AttachThingPrincipalAsync(request); _logger.LogInformation($"Attached certificate {certificateArn} to Thing {thingName}"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot attach certificate - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't attach certificate to Thing. Here's why: {ex.Message}"); return false; } }-
Einzelheiten zur API finden Sie AttachThingPrincipalin der AWS SDK für .NETAPI-Referenz.
-
- C++
-
- SDK für C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS-Code-Beispiel-
einrichten und ausführen. //! 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(); }-
Einzelheiten zur API finden Sie AttachThingPrincipalin der AWS SDK für C++API-Referenz.
-
- CLI
-
- AWS CLI
-
So hängen Sie Ihrem Objekt ein Zertifikat an
Im folgenden
attach-thing-principalBeispiel wird ein Zertifikat an das MyTemperatureSensor Ding angehängt. Das Zertifikat wird durch einen ARN identifiziert. Sie finden den ARN für ein Zertifikat in der AWS IoT-Konsole.aws iot attach-thing-principal \ --thing-nameMyTemperatureSensor\ --principalarn:aws:iot:us-west-2:123456789012:cert/2e1eb273792174ec2b9bf4e9b37e6c6c692345499506002a35159767055278e8Mit diesem Befehl wird keine Ausgabe zurückgegeben.
Weitere Informationen finden Sie unter Objektverwaltung mit der Registry im Entwicklerhandbuch für AWS IoT.
-
Einzelheiten zur API finden Sie AttachThingPrincipal
in der AWS CLIBefehlsreferenz.
-
- Java
-
- SDK für Java 2.x
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS-Code-Beispiel-
einrichten und ausführen. /** * 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(); }-
Einzelheiten zur API finden Sie AttachThingPrincipalin der AWS SDK for Java 2.xAPI-Referenz.
-
- Kotlin
-
- SDK für Kotlin
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS-Code-Beispiel-
einrichten und ausführen. suspend fun attachCertificateToThing( thingNameVal: String?, certificateArn: String?, ) { val principalRequest = AttachThingPrincipalRequest { thingName = thingNameVal principal = certificateArn } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.attachThingPrincipal(principalRequest) println("Certificate attached to $thingNameVal successfully.") } }-
Einzelheiten zur API finden Sie AttachThingPrincipal
in der API-Referenz zum AWS SDK für Kotlin.
-
Eine vollständige Liste der AWS SDK-Entwicklerhandbücher und Codebeispiele finden Sie unterVerwendung AWS IoT mit einem SDK AWS. Dieses Thema enthält auch Informationen zu den ersten Schritten und Details zu früheren SDK-Versionen.