DeleteCertificate 搭配 AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DeleteCertificate 搭配 AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 DeleteCertificate

.NET
適用於 .NET 的 SDK (v4)
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/// <summary> /// Deletes an IoT certificate. /// </summary> /// <param name="certificateId">The ID of the certificate to delete.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> DeleteCertificateAsync(string certificateId) { try { // First, update the certificate to inactive state var updateRequest = new UpdateCertificateRequest { CertificateId = certificateId, NewStatus = CertificateStatus.INACTIVE }; await _amazonIoT.UpdateCertificateAsync(updateRequest); // Then delete the certificate var deleteRequest = new DeleteCertificateRequest { CertificateId = certificateId }; await _amazonIoT.DeleteCertificateAsync(deleteRequest); _logger.LogInformation($"Deleted certificate {certificateId}"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot delete certificate - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't delete certificate. Here's why: {ex.Message}"); return false; } }
  • 如需 API 詳細資訊,請參閱《適用於 .NET 的 AWS SDK API 參考》中的 DeleteCertificate

C++
適用於 C++ 的 SDK
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

//! Delete a certificate. /*! \param certificateID: The ID of a certificate. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteCertificate(const Aws::String &certificateID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteCertificateRequest request; request.SetCertificateId(certificateID); Aws::IoT::Model::DeleteCertificateOutcome outcome = iotClient.DeleteCertificate( request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted certificate " << certificateID << std::endl; } else { std::cerr << "Error deleting certificate " << certificateID << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 如需 API 詳細資訊,請參閱《適用於 C++ 的 AWS SDK API 參考》中的 DeleteCertificate

CLI
AWS CLI

刪除裝置憑證

下列 delete-certificate 範例會刪除具有指定 ID 的裝置憑證。

aws iot delete-certificate \ --certificate-id c0c57bbc8baaf4631a9a0345c957657f5e710473e3ddbee1428d216d54d53ac9

此命令不會產生輸出。

如需詳細資訊,請參閱《AWS IoT API 參考》中的 DeleteCertificate

  • 如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 DeleteCertificate

Java
SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Deletes a certificate asynchronously. * * @param certificateArn The ARN of the certificate to delete. * * This method initiates an asynchronous request to delete a certificate. * If the deletion is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void deleteCertificate(String certificateArn) { DeleteCertificateRequest certificateProviderRequest = DeleteCertificateRequest.builder() .certificateId(extractCertificateId(certificateArn)) .build(); CompletableFuture<DeleteCertificateResponse> future = getAsyncClient().deleteCertificate(certificateProviderRequest); future.whenComplete((voidResult, ex) -> { if (ex == null) { System.out.println(certificateArn + " was successfully deleted."); } else { Throwable cause = ex.getCause(); if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + ex.getMessage()); } } }); future.join(); }
  • 如需 API 詳細資訊,請參閱《AWS SDK for Java 2.x API 參考》中的 DeleteCertificate

Kotlin
適用於 Kotlin 的 SDK
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun deleteCertificate(certificateArn: String) { val certificateProviderRequest = DeleteCertificateRequest { certificateId = extractCertificateId(certificateArn) } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.deleteCertificate(certificateProviderRequest) println("$certificateArn was successfully deleted.") } }
  • 如需 API 詳細資訊,請參閱《適用於 Kotlin 的AWS SDK API 參考》中的 DeleteCertificate