本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateThing 搭配 AWSSDK 或 CLI 使用
下列程式碼範例示範如何使用 CreateThing。
- .NET
-
- 適用於 .NET 的 SDK(v4)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /// <summary> /// Creates an AWS IoT Thing. /// </summary> /// <param name="thingName">The name of the Thing to create.</param> /// <returns>The ARN of the Thing created, or null if creation failed.</returns> public async Task<string?> CreateThingAsync(string thingName) { try { var request = new CreateThingRequest { ThingName = thingName }; var response = await _amazonIoT.CreateThingAsync(request); _logger.LogInformation($"Created Thing {thingName} with ARN {response.ThingArn}"); return response.ThingArn; } catch (Amazon.IoT.Model.ResourceAlreadyExistsException ex) { _logger.LogWarning($"Thing {thingName} already exists: {ex.Message}"); return null; } catch (Exception ex) { _logger.LogError($"Couldn't create Thing {thingName}. Here's why: {ex.Message}"); return null; } }-
如需 API 詳細資訊,請參閱《適用於 .NET 的 AWS SDK API 參考》中的 CreateThing。
-
- C++
-
- 適用於 C++ 的 SDK
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Create an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateThingRequest createThingRequest; createThingRequest.SetThingName(thingName); Aws::IoT::Model::CreateThingOutcome outcome = iotClient.CreateThing( createThingRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created thing " << thingName << std::endl; } else { std::cerr << "Failed to create thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }-
如需 API 詳細資訊,請參閱《適用於 C++ 的 AWS SDK API 參考》中的 CreateThing。
-
- CLI
-
- AWS CLI
-
範例 1:在登錄檔中建立物件記錄
下列
create-thing範例會在 AWSIoT 物件登錄檔中為裝置建立項目。aws iot create-thing \ --thing-nameSampleIoTThing輸出:
{ "thingName": "SampleIoTThing", "thingArn": "arn:aws:iot:us-west-2: 123456789012:thing/SampleIoTThing", "thingId": " EXAMPLE1-90ab-cdef-fedc-ba987EXAMPLE " }範例 2:定義與實物類型相關聯的實物
下列
create-thing範例會建立具有指定實物類型及其屬性的實物。aws iot create-thing \ --thing-name"MyLightBulb"\ --thing-type-name"LightBulb"\ --attribute-payload "{"attributes": {"wattage":"75", "model":"123"}}"輸出:
{ "thingName": "MyLightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyLightBulb", "thingId": "40da2e73-c6af-406e-b415-15acae538797" }如需詳細資訊,請參閱《AWS IoT 開發人員指南》中的如何使用登錄檔管理實物和實物類型。
-
如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 CreateThing
。
-
- Java
-
- SDK for Java 2.x
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * Creates an IoT Thing with the specified name asynchronously. * * @param thingName The name of the IoT Thing to create. * * This method initiates an asynchronous request to create an IoT Thing with the specified name. * If the request is successful, it prints the name of the thing and its ARN value. * If an exception occurs, it prints the error message. */ public void createIoTThing(String thingName) { CreateThingRequest createThingRequest = CreateThingRequest.builder() .thingName(thingName) .build(); CompletableFuture<CreateThingResponse> future = getAsyncClient().createThing(createThingRequest); future.whenComplete((createThingResponse, ex) -> { if (createThingResponse != null) { System.out.println(thingName + " was successfully created. The ARN value is " + createThingResponse.thingArn()); } else { Throwable cause = ex.getCause(); if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + cause.getMessage()); } } }); future.join(); }-
如需 API 詳細資訊,請參閱《AWS SDK for Java 2.x API 參考》中的 CreateThing。
-
- Kotlin
-
- 適用於 Kotlin 的 SDK
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 suspend fun createIoTThing(thingNameVal: String) { val createThingRequest = CreateThingRequest { thingName = thingNameVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.createThing(createThingRequest) println("Created $thingNameVal}") } }-
如需 API 詳細資訊,請參閱《適用於 Kotlin 的 AWS SDK API 參考》中的 CreateThing
。
-
如需 AWSSDK 開發人員指南和程式碼範例的完整清單,請參閱 AWS IoT搭配 AWSSDK 使用。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。