Use CreateThing com um AWS SDK ou CLI - AWS IoT Core

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Use CreateThing com um AWS SDK ou CLI

Os exemplos de código a seguir mostram como usar o CreateThing.

.NET
SDK para .NET(v4)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWSCode Examples Repository.

/// <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; } }
  • Para obter detalhes da API, consulte CreateThinga Referência AWS SDK para .NET da API.

C++
SDK para C++
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWSCode Examples Repository.

//! 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(); }
  • Para obter detalhes da API, consulte CreateThinga Referência AWS SDK para C++ da API.

CLI
AWS CLI

Exemplo 1: criar um registro de item no registro

O create-thing exemplo a seguir cria uma entrada para um dispositivo no registro de coisas da AWS IoT.

aws iot create-thing \ --thing-name SampleIoTThing

Saída:

{ "thingName": "SampleIoTThing", "thingArn": "arn:aws:iot:us-west-2: 123456789012:thing/SampleIoTThing", "thingId": " EXAMPLE1-90ab-cdef-fedc-ba987EXAMPLE " }

Exemplo 2: definir um item associado a um tipo de item

O exemplo create-thing a seguir cria um item com o tipo de item e seus atributos especificados.

aws iot create-thing \ --thing-name "MyLightBulb" \ --thing-type-name "LightBulb" \ --attribute-payload "{"attributes": {"wattage":"75", "model":"123"}}"

Saída:

{ "thingName": "MyLightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyLightBulb", "thingId": "40da2e73-c6af-406e-b415-15acae538797" }

Para obter mais informações, consulte Como gerenciar itens com o registro e Tipos de itens no Guia do desenvolvedor do AWS IoT.

  • Para obter detalhes da API, consulte CreateThingem Referência de AWS CLI Comandos.

Java
SDK para Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWSCode Examples Repository.

/** * 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(); }
  • Para obter detalhes da API, consulte CreateThinga Referência AWS SDK for Java 2.x da API.

Kotlin
SDK para Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWSCode Examples Repository.

suspend fun createIoTThing(thingNameVal: String) { val createThingRequest = CreateThingRequest { thingName = thingNameVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.createThing(createThingRequest) println("Created $thingNameVal}") } }
  • Para obter detalhes da API, consulte a CreateThingreferência da API AWSSDK for Kotlin.

Para obter uma lista completa dos guias do desenvolvedor do AWS SDK e exemplos de código, consulteUsando AWS IoT com um AWS SDK. Este tópico também inclui informações sobre como começar e detalhes sobre versões anteriores do SDK.