文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
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
-
- SDK for Java 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
-
- SDK for Kotlin
-
注意
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 詳細資訊,請參閱 AWS SDK for Kotlin API 參考中的 ListCertificates
。
-
DetachThingPrincipal
ListThings