

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立 Storage Lens 群組
<a name="storage-lens-groups-create"></a>

下列範例示範如何使用 Amazon S3 主控台 AWS Command Line Interface (AWS CLI) 和 建立 Amazon S3 Storage Lens 群組 適用於 Java 的 AWS SDK。

## 使用 S3 主控台
<a name="create-storage-lens-group-console"></a>

**建立 Storage Lens 群組**

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 的 Amazon S3 主控台。

1. 在頁面頂端的導覽列中，選擇目前顯示的 AWS 區域名稱。接下來，選擇您想要切換到的區域。

1. 在左側導覽窗格中，選擇 **Storage Lens 群組**。

1. 選擇**建立 Storage Lens 群組**。

1. 在**一般**下，檢視您的**主要區域**，並輸入您的 **Storage Lens 群組名稱**。

1. 在**範圍**底下，選擇要套用至 Storage Lens 群組的篩選條件。若要套用多個篩選條件，請選擇篩選條件，然後選擇 **AND** 或 **OR** 邏輯運算子。
   + 針對**字首**篩選條件，選擇**字首**，然後輸入字首字串。若要新增多個字首，請選擇**新增字首**。若要移除字首，請選擇要移除的字首旁的**移除**。
   + 在**物件標籤**篩選條件中，選擇**物件標籤**，然後輸入物件的鍵/值對。然後，選擇**新增標籤**。若要移除標籤，請選擇要移除的標籤旁的**移除**。
   + 針對**尾碼**篩選條件，選擇**尾碼**，然後輸入尾碼字串。若要新增多個尾碼，請選擇**新增尾碼**。若要移除尾碼，請選擇要移除的尾碼旁的**移除**。
   + 針對**存留期**篩選條件，指定物件存留期範圍 (天數)。選擇**指定最短物件存留期**，然後輸入最短的物件存留期。然後，選擇**指定最長物件存留期**，並輸入最長的物件存留期。
   + 針對**大小**篩選條件，指定物件大小範圍和測量單位。選擇**指定最小物件大小**，然後輸入最小的物件大小。選擇**指定最大物件大小**，並輸入最大的物件大小。

1. （選用） 對於 AWS 資源標籤，新增鍵/值對，然後選擇**新增標籤**。

1. 選擇**建立 Storage Lens 群組**。

## 使用 AWS CLI
<a name="create-storage-lens-group-cli"></a>

下列範例 AWS CLI 命令會建立 Storage Lens 群組。若要使用此範例命令，請以您自己的資訊取代 `{{user input placeholders}}`。

```
aws s3control create-storage-lens-group --account-id {{111122223333}} \ 
--region {{us-east-1}} --storage-lens-group=file://./{{marketing-department}}.json
```

下列範例 AWS CLI 命令會建立具有兩個 AWS 資源標籤的 Storage Lens 群組。若要使用此範例命令，請以您自己的資訊取代 `{{user input placeholders}}`。

```
aws s3control create-storage-lens-group --account-id {{111122223333}} \ 
--region {{us-east-1}} --storage-lens-group=file://./{{marketing-department}}.json \
--tags Key={{k1}},Value={{v1}} Key={{k2}},Value={{v2}}
```

如需範例 JSON 組態，請參閱 [Storage Lens 群組組態](storage-lens-groups.md#storage-lens-groups-configuration)。

## 使用適用於 Java 的 AWS 開發套件
<a name="create-storage-lens-group-sdk-java"></a>

下列 適用於 Java 的 AWS SDK 範例會建立 Storage Lens 群組。若要使用此範例，請以您自己的資訊取代 `{{user input placeholders}}`。

**Example – 使用單一篩選條件建立 Storage Lens 群組**  
下列範例會建立名為 `{{Marketing-Department}}` 的 Storage Lens 群組。此群組具有會將存留期範圍指定為 `{{30}}` 到 `{{90}}` 天的物件存留期篩選條件。若要使用此範例，請以您自己的資訊取代 `{{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.CreateStorageLensGroupRequest;
import software.amazon.awssdk.services.s3control.model.MatchObjectAge;
import software.amazon.awssdk.services.s3control.model.StorageLensGroup;
import software.amazon.awssdk.services.s3control.model.StorageLensGroupFilter;
 
public class CreateStorageLensGroupWithObjectAge {
    public static void main(String[] args) {
        String storageLensGroupName = "{{Marketing-Department}}";
        String accountId = "{{111122223333}}";
        
        try {
            StorageLensGroupFilter objectAgeFilter = StorageLensGroupFilter.builder()
                    .matchObjectAge(MatchObjectAge.builder()
                            .daysGreaterThan({{30}})
                            .daysLessThan({{90}})
                            .build())
                    .build();

            StorageLensGroup storageLensGroup = StorageLensGroup.builder()
                    .name({{storageLensGroupName}})
                    .filter({{objectAge}}Filter)
                    .build();

            CreateStorageLensGroupRequest createStorageLensGroupRequest = CreateStorageLensGroupRequest.builder()
                    .storageLensGroup({{storageLensGroup}})
                    .accountId({{accountId}}).build();

            S3ControlClient s3ControlClient = S3ControlClient.builder()
                    .region(Region.{{US_WEST_2}})
                    .credentialsProvider(ProfileCredentialsProvider.create())
                    .build();
            s3ControlClient.createStorageLensGroup(createStorageLensGroupRequest);
        } 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 – 使用包含多個篩選條件的 `AND` 運算子建立 Storage Lens 群組**  
下列範例會建立名為 `{{Marketing-Department}}` 的 Storage Lens 群組。此群組會使用 `AND` 運算子指出物件必須符合**所有**篩選條件。若要使用此範例，請以您自己的資訊取代 `{{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.CreateStorageLensGroupRequest;
import software.amazon.awssdk.services.s3control.model.MatchObjectAge;
import software.amazon.awssdk.services.s3control.model.MatchObjectSize;
import software.amazon.awssdk.services.s3control.model.S3Tag;
import software.amazon.awssdk.services.s3control.model.StorageLensGroup;
import software.amazon.awssdk.services.s3control.model.StorageLensGroupAndOperator;
import software.amazon.awssdk.services.s3control.model.StorageLensGroupFilter;


public class CreateStorageLensGroupWithAndFilter {
    public static void main(String[] args) {
        String storageLensGroupName = "{{Marketing-Department}}";
        String accountId = "{{111122223333}}";

        try {
            // Create object tags.
            S3Tag tag1 = S3Tag.builder()
                    .key("{{object-tag-key-1}}")
                    .value("{{object-tag-value-1}}")
                    .build();
            S3Tag tag2 = S3Tag.builder()
                    .key("{{object-tag-key-2}}")
                    .value("{{object-tag-value-2}}")
                    .build();

            StorageLensGroupAndOperator andOperator = StorageLensGroupAndOperator.builder()
                    .matchAnyPrefix("{{prefix-1}}", "{{prefix-2}}", "{{prefix-3/sub-prefix-1}}")
                    .matchAnySuffix("{{.png}}", "{{.gif}}", "{{.jpg}}")
                    .matchAnyTag({{tag1}}, {{tag2}})
                    .matchObjectAge(MatchObjectAge.builder()
                            .daysGreaterThan({{30}})
                            .daysLessThan({{90}}).build())
                    .matchObjectSize(MatchObjectSize.builder()
                            .bytesGreaterThan({{1000L}})
                            .bytesLessThan({{6000L}}).build())
                    .build();

            StorageLensGroupFilter andFilter = StorageLensGroupFilter.builder()
                    .and(andOperator)
                    .build();

            StorageLensGroup storageLensGroup = StorageLensGroup.builder()
                    .name({{storageLensGroupName}})
                    .filter(andFilter)
                    .build();

            CreateStorageLensGroupRequest createStorageLensGroupRequest = CreateStorageLensGroupRequest.builder()
                    .storageLensGroup({{storageLensGroup}})
                    .accountId({{accountId}}).build();

            S3ControlClient s3ControlClient = S3ControlClient.builder()
                    .region(Region.{{US_WEST_2}})
                    .credentialsProvider(ProfileCredentialsProvider.create())
                    .build();
            s3ControlClient.createStorageLensGroup(createStorageLensGroupRequest);
        } 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 – 使用包含多個篩選條件的 `OR` 運算子建立 Storage Lens 群組**  
下列範例會建立名為 `{{Marketing-Department}}` 的 Storage Lens 群組。此群組會使用 `OR` 運算子來套用字首篩選條件 (`{{prefix-1}}`、`{{prefix-2}}`、`{{prefix3/sub-prefix-1}}`) 或物件大小篩選條件，其大小範圍介於 `{{1000}}` 位元組與 `{{6000}}` 位元組之間。若要使用此範例，請以您自己的資訊取代 `{{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.CreateStorageLensGroupRequest;
import software.amazon.awssdk.services.s3control.model.MatchObjectSize;
import software.amazon.awssdk.services.s3control.model.StorageLensGroup;
import software.amazon.awssdk.services.s3control.model.StorageLensGroupFilter;
import software.amazon.awssdk.services.s3control.model.StorageLensGroupOrOperator;

public class CreateStorageLensGroupWithOrFilter {
    public static void main(String[] args) {
        String storageLensGroupName = "{{Marketing-Department}}";
        String accountId = "{{111122223333}}";

        try {
            StorageLensGroupOrOperator orOperator = StorageLensGroupOrOperator.builder()
                    .matchAnyPrefix("{{prefix-1}}", "{{prefix-2}}", "{{prefix-3/sub-prefix-1}}")
                    .matchObjectSize(MatchObjectSize.builder()
                            .bytesGreaterThan({{1000L}})
                            .bytesLessThan({{6000L}})
                            .build())
                    .build();

            StorageLensGroupFilter orFilter = StorageLensGroupFilter.builder()
                    .or(orOperator)
                    .build();

            StorageLensGroup storageLensGroup = StorageLensGroup.builder()
                    .name({{storageLensGroupName}})
                    .filter(orFilter)
                    .build();

            CreateStorageLensGroupRequest createStorageLensGroupRequest = CreateStorageLensGroupRequest.builder()
                    .storageLensGroup({{storageLensGroup}})
                    .accountId({{accountId}}).build();

            S3ControlClient s3ControlClient = S3ControlClient.builder()
                    .region(Region.{{US_WEST_2}})
                    .credentialsProvider(ProfileCredentialsProvider.create())
                    .build();
            s3ControlClient.createStorageLensGroup(createStorageLensGroupRequest);
        } 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 – 建立具有單一篩選條件和兩個 AWS 資源標籤的 Storage Lens 群組**  
下列範例會建立名為 `{{Marketing-Department}}`、具有尾碼篩選條件的 Storage Lens 群組。此範例也會將兩個 AWS 資源標籤新增至 Storage Lens 群組。若要使用此範例，請以您自己的資訊取代 `{{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.CreateStorageLensGroupRequest;
import software.amazon.awssdk.services.s3control.model.StorageLensGroup;
import software.amazon.awssdk.services.s3control.model.StorageLensGroupFilter;
import software.amazon.awssdk.services.s3control.model.Tag;

public class CreateStorageLensGroupWithResourceTags {
    public static void main(String[] args) {
        String storageLensGroupName = "{{Marketing-Department}}";
        String accountId = "{{111122223333}}";

        try {
            // Create AWS resource tags.
            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();

            StorageLensGroupFilter suffixFilter = StorageLensGroupFilter.builder()
                    .matchAnySuffix("{{.png}}", "{{.gif}}", ".{{jpg}}")
                    .build();

            StorageLensGroup storageLensGroup = StorageLensGroup.builder()
                    .name({{storageLensGroupName}})
                    .filter(suffixFilter)
                    .build();

            CreateStorageLensGroupRequest createStorageLensGroupRequest = CreateStorageLensGroupRequest.builder()
                    .storageLensGroup({{storageLensGroup}})
                    .tags({{resourceTag1}}, {{resourceTag2}})
                    .accountId({{accountId}}).build();

            S3ControlClient s3ControlClient = S3ControlClient.builder()
                    .region(Region.{{US_WEST_2}})
                    .credentialsProvider(ProfileCredentialsProvider.create())
                    .build();
            s3ControlClient.createStorageLensGroup(createStorageLensGroupRequest);
        } 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();
        }
    }
}
```

如需範例 JSON 組態，請參閱 [Storage Lens 群組組態](storage-lens-groups.md#storage-lens-groups-configuration)。