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

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

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

DescribeThing 搭配 AWS SDK 或 CLI 使用

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

C++
SDK for C++
注意

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

//! Describe an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::describeThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DescribeThingRequest request; request.SetThingName(thingName); Aws::IoT::Model::DescribeThingOutcome outcome = iotClient.DescribeThing(request); if (outcome.IsSuccess()) { const Aws::IoT::Model::DescribeThingResult &result = outcome.GetResult(); std::cout << "Retrieved thing '" << result.GetThingName() << "'" << std::endl; std::cout << "thingArn: " << result.GetThingArn() << std::endl; std::cout << result.GetAttributes().size() << " attribute(s) retrieved" << std::endl; for (const auto &attribute: result.GetAttributes()) { std::cout << " attribute: " << attribute.first << "=" << attribute.second << std::endl; } } else { std::cerr << "Error describing thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for C++ API 參考中的 DescribeThing

CLI
AWS CLI

顯示物件的詳細資訊

下列describe-thing範例顯示您 AWS 帳戶 AWS IoT 登錄檔中定義的物件 (裝置) 相關資訊。

aws iot describe-thing --thing-name "MyLightBulb"

輸出:

{ "defaultClientId": "MyLightBulb", "thingName": "MyLightBulb", "thingId": "40da2e73-c6af-406e-b415-15acae538797", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyLightBulb", "thingTypeName": "LightBulb", "attributes": { "model": "123", "wattage": "75" }, "version": 1 }

如需詳細資訊,請參閱 AWS IoT 開發人員指南中的如何使用登錄檔管理物件

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

Java
SDK for Java 2.x
注意

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

/** * Describes an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing. * * This method initiates an asynchronous request to describe an IoT Thing. * If the request is successful, it prints the Thing details. * If an exception occurs, it prints the error message. */ private void describeThing(String thingName) { DescribeThingRequest thingRequest = DescribeThingRequest.builder() .thingName(thingName) .build(); CompletableFuture<DescribeThingResponse> future = getAsyncClient().describeThing(thingRequest); future.whenComplete((describeResponse, ex) -> { if (describeResponse != null) { System.out.println("Thing Details:"); System.out.println("Thing Name: " + describeResponse.thingName()); System.out.println("Thing ARN: " + describeResponse.thingArn()); } 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 describe Thing."); } } }); future.join(); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DescribeThing

Kotlin
SDK for Kotlin
注意

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

suspend fun describeThing(thingNameVal: String) { val thingRequest = DescribeThingRequest { thingName = thingNameVal } // Print Thing details. IotClient { region = "us-east-1" }.use { iotClient -> val describeResponse = iotClient.describeThing(thingRequest) println("Thing details:") println("Thing name: ${describeResponse.thingName}") println("Thing ARN: ${describeResponse.thingArn}") } }
  • 如需 API 詳細資訊,請參閱適用於 AWS Kotlin 的 SDK API 參考中的 DescribeThing