与 AWS SDK或DeleteDocument一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或DeleteDocument一起使用 CLI

以下代码示例演示如何使用 DeleteDocument

CLI
AWS CLI

删除文档

以下 delete-document 示例删除一个 Systems Manager 文档。

aws ssm delete-document \ --name "Example"

此命令不生成任何输出。

有关更多信息,请参阅《AWS Systems Manager 用户指南》中的创建 Systems Manager 文档

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/** * Deletes an AWS SSM document asynchronously. * * @param documentName The name of the document to delete. * <p> * This method initiates an asynchronous request to delete an SSM document. * If an exception occurs, it handles the error appropriately. */ public void deleteDoc(String documentName) { DeleteDocumentRequest documentRequest = DeleteDocumentRequest.builder() .name(documentName) .build(); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { getAsyncClient().deleteDocument(documentRequest) .thenAccept(response -> { System.out.println("The SSM document was successfully deleted."); }) .exceptionally(ex -> { throw new CompletionException(ex); }).join(); }).exceptionally(ex -> { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof SsmException) { throw new RuntimeException("SSM error: " + cause.getMessage(), cause); } else { throw new RuntimeException("Unexpected error: " + cause.getMessage(), cause); } }); try { future.join(); } catch (CompletionException ex) { throw ex.getCause() instanceof RuntimeException ? (RuntimeException) ex.getCause() : ex; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteDocument” 中的。

JavaScript
SDK对于 JavaScript (v3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

import { DeleteDocumentCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Delete an SSM document. * @param {{ documentName: string }} */ export const main = async ({ documentName }) => { const client = new SSMClient({}); try { await client.send(new DeleteDocumentCommand({ Name: documentName })); console.log(`Document '${documentName}' deleted.`); return { Deleted: true }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide this value?`); } else { throw caught; } } };
  • 有关API详细信息,请参阅 “AWS SDK for JavaScript API参考 DeleteDocument” 中的。

PowerShell
用于 PowerShell

示例 1:此示例删除文档。如果此命令成功,则无任何输出。

Remove-SSMDocument -Name "RunShellScript"
  • 有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考DeleteDocument中的。

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class DocumentWrapper: """Encapsulates AWS Systems Manager Document actions.""" def __init__(self, ssm_client): """ :param ssm_client: A Boto3 Systems Manager client. """ self.ssm_client = ssm_client self.name = None @classmethod def from_client(cls): ssm_client = boto3.client("ssm") return cls(ssm_client) def delete(self): """ Deletes an AWS Systems Manager document. """ if self.name is None: return try: self.ssm_client.delete_document(Name=self.name) print(f"Deleted document {self.name}.") self.name = None except ClientError as err: logger.error( "Couldn't delete %s. Here's why: %s: %s", self.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • 有关API详细信息,请参阅DeleteDocument中的 AWS SDKPython (Boto3) API 参考。