ListCertificates与 AWS SDK 或 CLI 配合使用 - AWS SDK 代码示例

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

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

ListCertificates与 AWS SDK 或 CLI 配合使用

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

C++
SDK for C++
注意

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

//! List certificates registered in the AWS account making the call. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::listCertificates( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::ListCertificatesRequest request; Aws::Vector<Aws::IoT::Model::Certificate> allCertificates; Aws::String marker; // Used to paginate results. do { if (!marker.empty()) { request.SetMarker(marker); } Aws::IoT::Model::ListCertificatesOutcome outcome = iotClient.ListCertificates( request); if (outcome.IsSuccess()) { const Aws::IoT::Model::ListCertificatesResult &result = outcome.GetResult(); marker = result.GetNextMarker(); allCertificates.insert(allCertificates.end(), result.GetCertificates().begin(), result.GetCertificates().end()); } else { std::cerr << "Error: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!marker.empty()); std::cout << allCertificates.size() << " certificate(s) found." << std::endl; for (auto &certificate: allCertificates) { std::cout << "Certificate ID: " << certificate.GetCertificateId() << std::endl; std::cout << "Certificate ARN: " << certificate.GetCertificateArn() << std::endl; std::cout << std::endl; } return true; }
  • 有关 API 的详细信息,请参阅 AWS SDK for C++ API 参考ListCertificates中的。

CLI
AWS CLI

示例 1:列出在您的 AWS 账户中注册的证书

以下 list-certificates 示例列出在您的账户中注册的所有证书。如果您的分页数超过默认限制值 25,则您可以使用此命令中的 nextMarker 响应值并将其提供给下一个命令来获取下一批结果。重复此操作,直到 nextMarker 返回时没有值。

aws iot list-certificates

输出:

{ "certificates": [ { "certificateArn": "arn:aws:iot:us-west-2:123456789012:cert/604c48437a57b7d5fc5d137c5be75011c6ee67c9a6943683a1acb4b1626bac36", "certificateId": "604c48437a57b7d5fc5d137c5be75011c6ee67c9a6943683a1acb4b1626bac36", "status": "ACTIVE", "creationDate": 1556810537.617 }, { "certificateArn": "arn:aws:iot:us-west-2:123456789012:cert/262a1ac8a7d8aa72f6e96e365480f7313aa9db74b8339ec65d34dc3074e1c31e", "certificateId": "262a1ac8a7d8aa72f6e96e365480f7313aa9db74b8339ec65d34dc3074e1c31e", "status": "ACTIVE", "creationDate": 1546447050.885 }, { "certificateArn": "arn:aws:iot:us-west-2:123456789012:cert/b193ab7162c0fadca83246d24fa090300a1236fe58137e121b011804d8ac1d6b", "certificateId": "b193ab7162c0fadca83246d24fa090300a1236fe58137e121b011804d8ac1d6b", "status": "ACTIVE", "creationDate": 1546292258.322 }, { "certificateArn": "arn:aws:iot:us-west-2:123456789012:cert/7aebeea3845d14a44ec80b06b8b78a89f3f8a706974b8b34d18f5adf0741db42", "certificateId": "7aebeea3845d14a44ec80b06b8b78a89f3f8a706974b8b34d18f5adf0741db42", "status": "ACTIVE", "creationDate": 1541457693.453 }, { "certificateArn": "arn:aws:iot:us-west-2:123456789012:cert/54458aa39ebb3eb39c91ffbbdcc3a6ca1c7c094d1644b889f735a6fc2cd9a7e3", "certificateId": "54458aa39ebb3eb39c91ffbbdcc3a6ca1c7c094d1644b889f735a6fc2cd9a7e3", "status": "ACTIVE", "creationDate": 1541113568.611 }, { "certificateArn": "arn:aws:iot:us-west-2:123456789012:cert/4f0ba725787aa94d67d2fca420eca022242532e8b3c58e7465c7778b443fd65e", "certificateId": "4f0ba725787aa94d67d2fca420eca022242532e8b3c58e7465c7778b443fd65e", "status": "ACTIVE", "creationDate": 1541022751.983 } ] }
  • 有关 API 的详细信息,请参阅AWS CLI 命令参考ListCertificates中的。

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

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

/** * Lists all certificates asynchronously. * * This method initiates an asynchronous request to list all certificates. * If the request is successful, it prints the certificate IDs and ARNs. * If an exception occurs, it prints the error message. */ public void listCertificates() { CompletableFuture<ListCertificatesResponse> future = getAsyncClient().listCertificates(); future.whenComplete((response, ex) -> { if (response != null) { List<Certificate> certList = response.certificates(); for (Certificate cert : certList) { System.out.println("Cert id: " + cert.certificateId()); System.out.println("Cert Arn: " + cert.certificateArn()); } } 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 list certificates."); } } }); future.join(); }
  • 有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考ListCertificates中的。

Kotlin
适用于 Kotlin 的 SDK
注意

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

suspend fun listCertificates() { IotClient { region = "us-east-1" }.use { iotClient -> val response = iotClient.listCertificates() val certList = response.certificates certList?.forEach { cert -> println("Cert id: ${cert.certificateId}") println("Cert Arn: ${cert.certificateArn}") } } }
  • 有关 API 的详细信息,请参阅适用ListCertificates于 K otlin 的AWS SDK API 参考