Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Úselo CreateThing con un AWS SDK o CLI
Los siguientes ejemplos de código muestran cómo utilizar CreateThing.
- .NET
-
- SDK para .NET(v4)
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }-
Para obtener más información sobre la API, consulta CreateThingla Referencia AWS SDK para .NET de la API.
-
- C++
-
- SDK para C++
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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(); }-
Para obtener más información sobre la API, consulta CreateThingla Referencia AWS SDK para C++ de la API.
-
- CLI
-
- AWS CLI
-
Ejemplo 1: creación de un registro de objetos en el registro
El siguiente
create-thingejemplo crea una entrada para un dispositivo en el registro de cosas de AWS IoT.aws iot create-thing \ --thing-nameSampleIoTThingSalida:
{ "thingName": "SampleIoTThing", "thingArn": "arn:aws:iot:us-west-2: 123456789012:thing/SampleIoTThing", "thingId": " EXAMPLE1-90ab-cdef-fedc-ba987EXAMPLE " }Ejemplo 2: definición de un objeto que está asociado a un tipo de objeto
En el siguiente ejemplo de
create-thingse crea un objeto que tiene el tipo de objeto especificado y sus atributos.aws iot create-thing \ --thing-name"MyLightBulb"\ --thing-type-name"LightBulb"\ --attribute-payload "{"attributes": {"wattage":"75", "model":"123"}}"Salida:
{ "thingName": "MyLightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyLightBulb", "thingId": "40da2e73-c6af-406e-b415-15acae538797" }Para obtener más información, consulte How to Manage Things with the Registry y Thing Types en la Guía para desarrolladores de AWS IoT.
-
Para obtener más información sobre la API, consulte CreateThing
la Referencia de AWS CLI comandos.
-
- Java
-
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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(); }-
Para obtener más información sobre la API, consulta CreateThingla Referencia AWS SDK for Java 2.x de la API.
-
- Kotlin
-
- SDK para Kotlin
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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}") } }-
Para obtener más información sobre la API, consulta CreateThing
la referencia sobre el AWS SDK para la API de Kotlin.
-
Para ver una lista completa de guías para desarrolladores del AWS SDK y ejemplos de código, consultaSe utiliza AWS IoT con un SDK AWS. En este tema también se incluye información sobre cómo comenzar a utilizar el SDK y detalles sobre sus versiones anteriores.