本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
DeleteThing与 Amazon SDK 或 CLI 配合使用
以下代码示例演示如何使用 DeleteThing。
- .NET
-
- 适用于 .NET 的 Amazon SDK (v4)
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /// <summary> /// Deletes an IoT Thing. /// </summary> /// <param name="thingName">The name of the Thing to delete.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> DeleteThingAsync(string thingName) { try { var request = new DeleteThingRequest { ThingName = thingName }; await _amazonIoT.DeleteThingAsync(request); _logger.LogInformation($"Deleted Thing {thingName}"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot delete Thing - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't delete Thing. Here's why: {ex.Message}"); return false; } }-
有关 API 的详细信息,请参阅 适用于 .NET 的 Amazon SDK API 参考DeleteThing中的。
-
- C++
-
- SDK for C++
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 //! Delete an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteThingRequest request; request.SetThingName(thingName); const auto outcome = iotClient.DeleteThing(request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted thing " << thingName << std::endl; } else { std::cerr << "Error deleting thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }-
有关 API 的详细信息,请参阅 适用于 C++ 的 Amazon SDK API 参考DeleteThing中的。
-
- CLI
-
- Amazon CLI
-
显示有关事物的详细信息
以下
delete-thing示例从您 Amazon 账户的 Amazon IoT 注册表中删除一个事物。aws iot 删除-thing-thing-name “” FourthBulb
此命令不生成任何输出。
有关更多信息,请参阅《Amazon IoT 开发人员指南》中的如何使用注册表管理事物。
-
有关 API 的详细信息,请参阅Amazon CLI 命令参考DeleteThing
中的。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /** * Deletes an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing to delete. * * This method initiates an asynchronous request to delete an IoT Thing. * If the deletion is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void deleteIoTThing(String thingName) { DeleteThingRequest deleteThingRequest = DeleteThingRequest.builder() .thingName(thingName) .build(); CompletableFuture<DeleteThingResponse> future = getAsyncClient().deleteThing(deleteThingRequest); future.whenComplete((voidResult, ex) -> { if (ex == null) { System.out.println("Deleted Thing " + thingName); } else { Throwable cause = ex.getCause(); if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + ex.getMessage()); } } }); future.join(); }-
有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考DeleteThing中的。
-
- Kotlin
-
- 适用于 Kotlin 的 SDK
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 suspend fun deleteIoTThing(thingNameVal: String) { val deleteThingRequest = DeleteThingRequest { thingName = thingNameVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.deleteThing(deleteThingRequest) println("Deleted $thingNameVal") } }-
有关 API 的详细信息,请参阅适用DeleteThing
于 K otlin 的Amazon SDK API 参考。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 class IoTWrapper: """Encapsulates AWS IoT actions.""" def __init__(self, iot_client, iot_data_client=None): """ :param iot_client: A Boto3 AWS IoT client. :param iot_data_client: A Boto3 AWS IoT Data Plane client. """ self.iot_client = iot_client self.iot_data_client = iot_data_client @classmethod def from_client(cls): iot_client = boto3.client("iot") iot_data_client = boto3.client("iot-data") return cls(iot_client, iot_data_client) def delete_thing(self, thing_name): """ Deletes an AWS IoT thing. :param thing_name: The name of the thing to delete. """ try: self.iot_client.delete_thing(thingName=thing_name) logger.info("Deleted thing %s.", thing_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Cannot delete thing. Resource not found.") return logger.error( "Couldn't delete thing. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise-
有关 API 的详细信息,请参阅适用DeleteThing于 Python 的Amazon SDK (Boto3) API 参考。
-
有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅Amazon IoT 与 Amazon SDK 一起使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。