

# 向 Storage Lens 组添加 Amazon 资源标签
<a name="storage-lens-groups-add-tags"></a>

以下示例演示如何将 Amazon 资源标签添加到 Amazon S3 Storage Lens 存储统计管理工具组。您可以使用 Amazon S3 控制台、Amazon Command Line Interface (Amazon CLI) 和 适用于 Java 的 Amazon SDK 添加资源标签。

## 使用 S3 控制台
<a name="storage-lens-groups-add-tags-console"></a>

**向 Storage Lens 组添加 Amazon 资源标签**

1. 登录到 Amazon Web Services 管理控制台，然后通过以下网址打开 Amazon S3 控制台：[https://console.aws.amazon.com/s3/](https://console.amazonaws.cn/s3/)。

1. 在左侧导航窗格中，选择 **Storage Lens 组**。

1. 在 **Storage Lens 组**下，选择要更新的 Storage Lens 组。

1. 在 **Amazon 资源标签**下，选择**添加标签**。

1. 在**添加标签**页面上，添加新的键值对。
**注意**  
如果添加的新标签与现有标签具有相同的键，则将覆盖之前的标签值。

1. （可选）要添加多个新标签，请再次选择**添加标签**以继续添加新条目。您最多可以将 50 个 Amazon 资源标签添加到 Storage Lens 组。

1. （可选）如果要移除新添加的条目，请选择要移除的标签旁的**移除**。

1. 选择**保存更改**。

## 使用 Amazon CLI
<a name="storage-lens-groups-add-tags-cli"></a>

以下示例 Amazon CLI 命令将向名为 `{{marketing-department}}` 的现有 Storage Lens 组添加两个资源标签。要使用此示例命令，请将 `{{user input placeholders}}` 替换为您自己的信息。

```
aws s3control tag-resource --account-id {{111122223333}} \
--resource-arn arn:aws:s3:{{us-east-1}}:{{111122223333}}:storage-lens-group/{{marketing-department}} \
--region {{us-east-1}} --tags Key={{k1}},Value={{v1}} Key={{k2}},Value={{v2}}
```

## 使用适用于 Java 的 Amazon SDK
<a name="storage-lens-groups-add-tags-sdk-java"></a>

以下 适用于 Java 的 Amazon SDK 示例将向现有 Storage Lens 组添加两个 Amazon 资源标签。要使用此示例，请将 `{{user input placeholders}}` 替换为您自己的信息。

```
package aws.example.s3control;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3control.S3ControlClient;
import software.amazon.awssdk.services.s3control.model.Tag;
import software.amazon.awssdk.services.s3control.model.TagResourceRequest;

public class TagResource {
    public static void main(String[] args) {
        String resourceARN = "{{Resource_ARN}}";
        String accountId = "{{111122223333}}";

        try {
            Tag resourceTag1 = Tag.builder()
                .key("{{resource-tag-key-1}}")
                .value("{{resource-tag-value-1}}")
                .build();
            Tag resourceTag2 = Tag.builder()
                    .key("{{resource-tag-key-2}}")
                    .value("{{resource-tag-value-2}}")
                    .build();
            TagResourceRequest tagResourceRequest = TagResourceRequest.builder()
                    .resourceArn({{resourceARN}})
                    .tags({{resourceTag1}}, {{resourceTag2}})
                    .accountId({{accountId}})
                    .build();
            S3ControlClient s3ControlClient = S3ControlClient.builder()
                    .region(Region.{{US_WEST_2}})
                    .credentialsProvider(ProfileCredentialsProvider.create())
                    .build();
            s3ControlClient.tagResource(tagResourceRequest);
        } catch (AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process
            // it and returned an error response.
            e.printStackTrace();
        } catch (SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }
    }
}
```