本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
TagResource
与 Amazon SDK 或 CLI 配合使用
以下代码示例演示如何使用 TagResource
。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- CLI
-
- Amazon CLI
-
向 KMS 密钥添加标签
以下
tag-resource
示例为客户托管的 KMS 密钥添加"Purpose":"Test"
和"Dept":"IT"
标签。您可以使用此类标签来标记 KMS 密钥并创建 KMS 密钥类别以进行权限管理和审计。要指定 KMS 密钥,请使用
key-id
参数。此示例使用密钥 ID 值,但您可以在此命令中使用密钥 ID 或密钥 ARN。aws kms tag-resource \ --key-id
1234abcd-12ab-34cd-56ef-1234567890ab
\ --tags TagKey='Purpose',TagValue='Test' TagKey='Dept',TagValue='IT'此命令不生成任何输出。要查看 Amazon KMS 密钥上的标签,请使用
list-resource-tags
comand。有关在 Amazon KMS 中使用标签的更多信息,请参阅《密钥管理服务开发人员指南》中的标记密Amazon 钥。
-
有关 API 的详细信息,请参阅Amazon CLI 命令参考TagResource
中的。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /** * Asynchronously tags a KMS key with a specific tag. * * @param keyId the ID of the KMS key to be tagged * @return a {@link CompletableFuture} that completes when the tagging operation is finished */ public CompletableFuture<Void> tagKMSKeyAsync(String keyId) { Tag tag = Tag.builder() .tagKey("Environment") .tagValue("Production") .build(); TagResourceRequest tagResourceRequest = TagResourceRequest.builder() .keyId(keyId) .tags(tag) .build(); return getAsyncClient().tagResource(tagResourceRequest) .thenRun(() -> { logger.info("{} key was tagged", keyId); }) .exceptionally(throwable -> { throw new RuntimeException("Failed to tag the KMS key", throwable); }); }
-
有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考TagResource中的。
-
- PHP
-
- 适用于 PHP 的 SDK
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /*** * @param string $keyId * @param array $tags * @return void */ public function tagResource(string $keyId, array $tags) { try { $this->client->tagResource([ 'KeyId' => $keyId, 'Tags' => $tags, ]); }catch(KmsException $caught){ echo "There was a problem applying the tag(s): {$caught->getAwsErrorMessage()}\n"; throw $caught; } }
-
有关 API 的详细信息,请参阅 适用于 PHP 的 Amazon SDK API 参考TagResource中的。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 class KeyManager: def __init__(self, kms_client): self.kms_client = kms_client self.created_keys = [] @classmethod def from_client(cls) -> "KeyManager": """ Creates a KeyManager instance with a default KMS client. :return: An instance of KeyManager initialized with the default KMS client. """ kms_client = boto3.client("kms") return cls(kms_client) def tag_resource(self, key_id: str, tag_key: str, tag_value: str) -> None: """ Add or edit tags on a customer managed key. :param key_id: The ARN or ID of the key to enable rotation for. :param tag_key: Key for the tag. :param tag_value: Value for the tag. """ try: self.kms_client.tag_resource( KeyId=key_id, Tags=[{"TagKey": tag_key, "TagValue": tag_value}] ) except ClientError as err: logging.error( "Couldn't add a tag for the key '%s'. Here's why: %s", key_id, err.response["Error"]["Message"], ) raise
-
有关 API 的详细信息,请参阅适用TagResource于 Python 的Amazon SDK (Boto3) API 参考。
-
有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅将此服务与 Amazon SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。