

# 将 S3 Storage Lens 组附加到控制面板或从控制面板移除
<a name="storage-lens-groups-dashboard-console"></a>

在 Amazon S3 Storage Lens 存储统计管理工具中升级到高级级别后，您可以将 [Storage Lens 组](https://docs.amazonaws.cn/AmazonS3/latest/userguide/storage-lens-groups-overview.html)附加到控制面板。如果您有多个 Storage Lens 组，则可以根据需要包括或排除组。

您的 Storage Lens 组必须位于控制面板账户中指定的主区域内。将 Storage Lens 组附加到控制面板后，您将在 48 小时内在指标导出中收到新增的 Storage Lens 组聚合数据。

**注意**  
如果要查看 Storage Lens 组的聚合指标，则必须将其附加到 Storage Lens 控制面板。有关 Storage Lens 组 JSON 配置文件的示例，请参阅 [JSON 格式的 S3 Storage Lens 存储统计管理工具示例配置（带 Storage Lens 组）](S3LensHelperFilesCLI.md#StorageLensGroupsHelperFilesCLI)。

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

**将 Storage Lens 组附加到 Storage Lens 控制面板**

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. 选择**编辑**。

1. 在 **Metrics selection**（指标选择）下方，请选择 **Advanced metrics and recommendations**（高级指标和建议）。

1. 选择 **Storage Lens 组聚合**。
**注意**  
默认情况下，还会选中**高级指标**。但是，您也可以取消选中此设置，因为聚合 Storage Lens 组数据并非必须。

1. 向下滚动到 **Storage Lens 组聚合**，然后指定要在数据聚合中包括或排除的一个或多个 Storage Lens 组。您可以使用以下筛选条件选项：
   + 如果要包括某些 Storage Lens 组，请选择**包括 Storage Lens 组**。在**要包括的 Storage Lens 组**下，选择您的 Storage Lens 组。
   + 如果要包括所有 Storage Lens 组，请选择**包括此账户中主区域中的所有 Storage Lens 组**。
   + 如果要排除某些 Storage Lens 组，请选择**排除 Storage Lens 组**。在**要排除的 Storage Lens 组**下，选择要排除的 Storage Lens 组。

1. 选择**保存更改**。如果您正确配置了 Storage Lens 组，则将在 48 小时内在控制面板中看到新增的 Storage Lens 组聚合数据。

**从 S3 Storage Lens 存储统计管理工具控制面板移除 Storage Lens 组**

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. 选择**查看控制面板配置**。

1. 选择**编辑**。

1. 向下滚动到**指标选择**部分。

1. 在 **Storage Lens 组聚合**下，选择要移除的 Storage Lens 组旁的 **X**。这将移除 Storage Lens 组。

   如果您在控制面板中包括了所有 Storage Lens 组，请清除**包括此账户中主区域中的所有 Storage Lens 组**旁的复选框。

1. 选择**保存更改**。
**注意**  
最多需要等待 48 小时，控制面板才会反映配置更新。

## 使用适用于 Java 的 Amazon SDK
<a name="StorageLensGroupsConfigurationJava"></a>

**Example – 将所有 Storage Lens 组附加到控制面板**  
以下 Java 版 SDK 示例将账户 {{111122223333}} 中的所有 Storage Lens 组附加到 {{DashBoardConfigurationId}} 控制面板：  

```
package aws.example.s3control;


import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3control.AWSS3Control;
import com.amazonaws.services.s3control.AWSS3ControlClient;
import com.amazonaws.services.s3control.model.BucketLevel;
import com.amazonaws.services.s3control.model.PutStorageLensConfigurationRequest;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3control.model.AccountLevel;
import com.amazonaws.services.s3control.model.StorageLensConfiguration;
import com.amazonaws.services.s3control.model.StorageLensGroupLevel;

import static com.amazonaws.regions.Regions.{{US_WEST_2}};

public class CreateDashboardWithStorageLensGroups {
    public static void main(String[] args) {
        String configurationId = "{{ExampleDashboardConfigurationId}}";
        String sourceAccountId = "{{111122223333}}";

        try {
            StorageLensGroupLevel storageLensGroupLevel = new StorageLensGroupLevel();

            AccountLevel accountLevel = new AccountLevel()
                    .withBucketLevel(new BucketLevel())
                    .withStorageLensGroupLevel({{storageLensGroupLevel}});

            StorageLensConfiguration configuration = new StorageLensConfiguration()
                    .withId(configurationId)
                    .withAccountLevel(accountLevel)
                    .withIsEnabled(true);

            AWSS3Control s3ControlClient = AWSS3ControlClient.builder()
                    .withCredentials(new ProfileCredentialsProvider())
                    .withRegion({{US_WEST_2}})
                    .build();

            s3ControlClient.putStorageLensConfiguration(new PutStorageLensConfigurationRequest()
                    .withAccountId(sourceAccountId)
                    .withConfigId(configurationId)
                    .withStorageLensConfiguration(configuration)
            );
        } 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();
        }
    }
}
```

**Example – 将两个 Storage Lens 组附加到控制面板**  
以下适用于 Java 的 Amazon SDK 示例将两个 Storage Lens 组（{{StorageLensGroupName1}} 和 {{StorageLensGroupName2}}）附加到 {{ExampleDashboardConfigurationId}} 控制面板。  

```
package aws.example.s3control;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3control.AWSS3Control;
import com.amazonaws.services.s3control.AWSS3ControlClient;
import com.amazonaws.services.s3control.model.AccountLevel;
import com.amazonaws.services.s3control.model.BucketLevel;
import com.amazonaws.services.s3control.model.PutStorageLensConfigurationRequest;
import com.amazonaws.services.s3control.model.StorageLensConfiguration;
import com.amazonaws.services.s3control.model.StorageLensGroupLevel;
import com.amazonaws.services.s3control.model.StorageLensGroupLevelSelectionCriteria;

import static com.amazonaws.regions.Regions.{{US_WEST_2}};

public class CreateDashboardWith2StorageLensGroups {
    public static void main(String[] args) {
        String configurationId = "{{ExampleDashboardConfigurationId}}";
        String storageLensGroupName1 = "{{StorageLensGroupName1}}";
        String storageLensGroupName2 = "{{StorageLensGroupName2}}";
        String sourceAccountId = "{{111122223333}}";

        try {
            StorageLensGroupLevelSelectionCriteria selectionCriteria = new StorageLensGroupLevelSelectionCriteria()
                    .withInclude(
                            "arn:aws:s3:" + {{US_WEST_2}}.getName() + ":" + sourceAccountId + ":storage-lens-group/" + {{storageLensGroupName1}},
                            "arn:aws:s3:" + {{US_WEST_2}}.getName() + ":" + sourceAccountId + ":storage-lens-group/" + {{storageLensGroupName2}});

            System.out.println(selectionCriteria);
            StorageLensGroupLevel storageLensGroupLevel = new StorageLensGroupLevel()
                    .withSelectionCriteria(selectionCriteria);

            AccountLevel accountLevel = new AccountLevel()
                    .withBucketLevel(new BucketLevel())
                    .withStorageLensGroupLevel(storageLensGroupLevel);

            StorageLensConfiguration configuration = new StorageLensConfiguration()
                    .withId(configurationId)
                    .withAccountLevel(accountLevel)
                    .withIsEnabled(true);

            AWSS3Control s3ControlClient = AWSS3ControlClient.builder()
                    .withCredentials(new ProfileCredentialsProvider())
                    .withRegion({{US_WEST_2}})
                    .build();

            s3ControlClient.putStorageLensConfiguration(new PutStorageLensConfigurationRequest()
                    .withAccountId(sourceAccountId)
                    .withConfigId(configurationId)
                    .withStorageLensConfiguration(configuration)
            );
        } 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();
        }
    }
}
```

**Example – 附加除排除项之外的所有 Storage Lens 组**  
以下 Java 版 SDK 示例将所有 Storage Lens 组附加到 {{ExampleDashboardConfigurationId}} 控制面板，但不包括指定的两个组（{{StorageLensGroupName1}} 和 {{StorageLensGroupName2}}）：  

```
package aws.example.s3control;


import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3control.AWSS3Control;
import com.amazonaws.services.s3control.AWSS3ControlClient;
import com.amazonaws.services.s3control.model.AccountLevel;
import com.amazonaws.services.s3control.model.BucketLevel;
import com.amazonaws.services.s3control.model.PutStorageLensConfigurationRequest;
import com.amazonaws.services.s3control.model.StorageLensConfiguration;
import com.amazonaws.services.s3control.model.StorageLensGroupLevel;
import com.amazonaws.services.s3control.model.StorageLensGroupLevelSelectionCriteria;

import static com.amazonaws.regions.Regions.{{US_WEST_2}};

public class CreateDashboardWith2StorageLensGroupsExcluded {
    public static void main(String[] args) {
        String configurationId = "{{ExampleDashboardConfigurationId}}";
        String storageLensGroupName1 = "{{StorageLensGroupName1}}";
        String storageLensGroupName2 = "{{StorageLensGroupName2}}";
        String sourceAccountId = "{{111122223333}}";

        try {
            StorageLensGroupLevelSelectionCriteria selectionCriteria = new StorageLensGroupLevelSelectionCriteria()
                    .withInclude(
                            "arn:aws:s3:" + {{US_WEST_2}}.getName() + ":" + sourceAccountId + ":storage-lens-group/" + {{storageLensGroupName1}},
                            "arn:aws:s3:" + {{US_WEST_2}}.getName() + ":" + sourceAccountId + ":storage-lens-group/" + {{storageLensGroupName2}});

            System.out.println(selectionCriteria);
            StorageLensGroupLevel storageLensGroupLevel = new StorageLensGroupLevel()
                    .withSelectionCriteria(selectionCriteria);

            AccountLevel accountLevel = new AccountLevel()
                    .withBucketLevel(new BucketLevel())
                    .withStorageLensGroupLevel(storageLensGroupLevel);

            StorageLensConfiguration configuration = new StorageLensConfiguration()
                    .withId(configurationId)
                    .withAccountLevel(accountLevel)
                    .withIsEnabled(true);

            AWSS3Control s3ControlClient = AWSS3ControlClient.builder()
                    .withCredentials(new ProfileCredentialsProvider())
                    .withRegion({{US_WEST_2}})
                    .build();

            s3ControlClient.putStorageLensConfiguration(new PutStorageLensConfigurationRequest()
                    .withAccountId(sourceAccountId)
                    .withConfigId(configurationId)
                    .withStorageLensConfiguration(configuration)
            );
        } 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();
        }
    }
}
```