本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
DeleteThing 搭配 AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 DeleteThing。
- .NET
-
- 適用於 .NET 的 SDK (v4)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /// <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 的 AWS SDK API 參考》中的 DeleteThing。
-
- C++
-
- 適用於 C++ 的 SDK
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! 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++ 的 AWS SDK API 參考》中的 DeleteThing。
-
- CLI
-
- AWS CLI
-
顯示詳細的物件資訊
下列
delete-thing範例會從您 AWS 帳戶的 AWS IoT 登錄檔中刪除物件。aws iot delete-thing --thing-name "FourthBulb"
此命令不會產生輸出。
如需詳細資訊,請參閱《AWS IoT 開發人員指南》中的如何使用登錄檔管理物件。
-
如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 DeleteThing
。
-
- Java
-
- SDK for Java 2.x
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * 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 詳細資訊,請參閱《AWS SDK for Java 2.x API 參考》中的 DeleteThing。
-
- Kotlin
-
- 適用於 Kotlin 的 SDK
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 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 詳細資訊,請參閱《適用於 Kotlin 的AWS SDK API 參考》中的 DeleteThing
。
-
- Python
-
- 適用於 Python 的 SDK (Boto3)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 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 詳細資訊,請參閱《適用於 AWS Python (Boto3) 的 SDK API 參考》中的 DeleteThing。
-
如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 AWS IoT 搭配 AWS SDK 使用。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。