TagResource与 AWS SDK 一起使用 - AWS SDK 代码示例

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

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

TagResource与 AWS SDK 一起使用

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

Java
适用于 Java 的 SDK 2.x
注意

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

/** * Tags the specified schema mapping ARN. * * @param schemaMappingARN the ARN of the schema mapping to tag */ public CompletableFuture<TagResourceResponse> tagEntityResource(String schemaMappingARN) { Map<String, String> tags = new HashMap<>(); tags.put("tag1", "tag1Value"); tags.put("tag2", "tag2Value"); TagResourceRequest request = TagResourceRequest.builder() .resourceArn(schemaMappingARN) .tags(tags) .build(); return getResolutionAsyncClient().tagResource(request) .whenComplete((response, exception) -> { if (response != null) { // Successfully tagged the resource, log the success message. logger.info("Successfully tagged the resource."); } else { if (exception == null) { throw new CompletionException("An unknown error occurred while tagging the resource.", null); } Throwable cause = exception.getCause(); if (cause instanceof ResourceNotFoundException) { throw new CompletionException("The resource to tag was not found.", cause); } throw new CompletionException("Failed to tag the resource: " + exception.getMessage(), exception); } }); }
  • 有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考TagResource中的。

JavaScript
适用于 JavaScript (v3) 的软件开发工具包
注意

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

//The default inputs for this demo are read from the ../inputs.json. import { fileURLToPath } from "node:url"; import { TagResourceCommand, EntityResolutionClient, } from "@aws-sdk/client-entityresolution"; import data from "../inputs.json" with { type: "json" }; const region = "eu-west-1"; const erClient = new EntityResolutionClient({ region: region }); export const main = async () => { const tagResourceCommandParams = { resourceArn: `${data.inputs.schemaArn}`, tags: { tag1: "tag1Value", tag2: "tag2Value", }, }; try { const command = new TagResourceCommand(tagResourceCommandParams); const response = await erClient.send(command); console.log("Successfully tagged the resource."); } catch (caught) { console.error(caught.message); throw caught; } };
  • 有关 API 的详细信息,请参阅 适用于 JavaScript 的 AWS SDK API 参考TagResource中的。