

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

# 啟用 S3 Storage Lens 的 CloudWatch 發佈
<a name="storage-lens-cloudwatch-enable-publish-option"></a>

您可以將 S3 Storage Lens 指標發佈到 Amazon CloudWatch，以在 [CloudWatch 儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)中建立統一的運作狀態檢視。您也可以使用 CloudWatch 功能 (例如警示和觸發動作、指標數學和異常偵測) 來監控 S3 Storage Lens 指標並對其採取動作。此外，CloudWatch API 操作可讓應用程式 (包括第三方供應商) 存取您的 S3 Storage Lens 指標。如需有關 CloudWatch 功能的詳細資訊，請參閱《[Amazon CloudWatch 使用者指南](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html)》。

S3 Storage Lens 指標會在擁有 S3 Storage Lens 組態的帳戶中發佈至 CloudWatch。在進階指標中啟用 CloudWatch 發佈選項後，您可以在 CloudWatch 中依組態 ID、帳戶、儲存貯體 （僅適用於儲存貯體層級指標）、區域和儲存類別存取帳戶層級和儲存貯體層級指標。在 CloudWatch 中不提供字首層級的指標。

您可以使用 S3 主控台、Amazon S3 REST APIs 和 AWS SDKs AWS CLI，為新的或現有的儀表板組態啟用 CloudWatch 支援。CloudWatch 發佈選項適用於升級至 S3 Storage Lens 進階指標和建議的儀表板。如需 S3 Storage Lens 進階指標和建議定價，請參閱 [Amazon S3 定價](https://aws.amazon.com/s3/pricing/)。不會產生額外的 CloudWatch 指標發佈費用；不過，儀表板、警示和 API 呼叫等其他 CloudWatch 費用則適用。

若要為 S3 Storage Lens 指標啟用 CloudWatch 發佈選項，請參閱下列主題。

**注意**  
S3 Storage Lens 指標是每日指標，每天向 CloudWatch 發佈一次。在 CloudWatch 中查詢 S3 Storage Lens 指標時，查詢的時段必須為 1 天 (86400 秒)。在每日 S3 Storage Lens 指標出現在 Amazon S3 主控台的 S3 Storage Lens 儀表板中之後，這些相同指標可能會需要幾個小時才會出現在 CloudWatch 中。首次啟用 S3 Storage Lens 指標的 CloudWatch 發佈選項時，您的指標最多可能需要 24 小時才能發佈到 CloudWatch。  
目前，S3 Storage Lens 指標無法透過 CloudWatch 串流取用。

## 使用 S3 主控台
<a name="storage-lens-cloudwatch-enable-publish-console"></a>

更新 S3 Storage Lens 儀表板時，您無法變更儀表板名稱或主要區域。您也無法變更預設儀表板的範圍，其範圍限定在整個帳戶的儲存體。

**更新 S3 Storage Lens 儀表板以啟用 CloudWatch 發佈**

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

1. 在左導覽窗格中，選擇 **S3 Storage Lens**、**Dashboards** (儀表板)。

1. 選擇您要編輯的儀表板，然後選擇 **Edit** (編輯)。

1. 在 **Metrics selection** (指標選擇) 下，選擇 **Advanced metrics and recommendations** (進階指標和建議)。

   進階指標和建議需支付額外費用。進階指標和建議包含資料查詢的 15 個月時段、字首層級彙總的用量指標、依儲存貯體彙總的活動指標、CloudWatch 發佈選項，以及協助您最佳化儲存成本，並套用資料保護最佳實務的內容相關建議。如需詳細資訊，請參閱 [Simple Storage Service (Amazon S3) 定價](https://aws.amazon.com/s3/pricing/)。

1. 在 **Select Advanced metrics and recommendations features** (選取進階指標和建議功能) 下，選取 **CloudWatch publishing** (CloudWatch 發佈)。
**重要**  
如果您的組態啟用用量指標的字首彙總，則字首層級指標將不會發佈至 CloudWatch。僅儲存貯體、帳戶和組織層級 S3 Storage Lens 指標發佈至 CloudWatch。

1. 選擇**儲存變更**。

**建立啟用 CloudWatch 支援的新 S3 Storage Lens 儀表板**

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

1. 在左導覽窗格中，選擇 **Storage Lens**、**Dashboards** (儀表板)。

1. 選擇 **Create dashboard (建立儀表板)**。

1. 在 **General** (一般) 下，定義下列組態選項：

   1. 針對 **Dashboard name** (儀表板名稱)，輸入儀表板名稱。

      儀表板名稱必須少於 65 個字元，且不得包含特殊字元或空格。建立儀表板後，您就無法變更儀表板名稱。

   1. 選擇儀表板的 **Home Region** (主要區域)。

      此儀表板範圍中所有包含區域的指標都會集中存放在此指定的主要區域。在 CloudWatch 中，主要區域也提供 S3 Storage Lens 指標。建立儀表板後，您就無法變更主要區域。

1. (選用) 若要新增標籤，請選擇 **Add tag** (新增標籤)，然後輸入標籤的 **Key** (鍵) 和 **Value** (值)。
**注意**  
您最多可為儀表板組態中新增 50 個標籤。

1. 定義您的組態範圍：

   1. 如果您要建立組織層級的組態，請選擇要包含在組態中的帳戶：**Include all accounts in your configuration** (將全部帳戶包含在組態中) 或者 **Limit the scope to your signed-in account** (將範圍限制在您的登入帳戶)。
**注意**  
建立包含所有帳戶的組織層級組態時，您只能包含或排除區域，而不是儲存貯體。

   1. 選擇您想要 S3 Storage Lens 在儀表板組態中包含的區域和儲存貯體，方法為執行下列動作：
      + 若要包含所有區域，請選擇 **Include Regions and buckets** (包含區域和儲存貯體)。
      + 若要包含特定區域，請清除 **Include all Regions** (包含所有區域)。在 **Choose Regions to include** (選擇要包含的區域) 下，選擇您想要 S3 Storage Lens 包含在儀表板中的區域。
      + 若要包含特定的儲存貯體，請清除 **Include all buckets** (包含所有儲存貯體)。在 **Choose buckets to include** (選擇要包含的儲存貯體) 下，選擇您想要 S3 Storage Lens 包含在儀表板中的儲存貯體。
**注意**  
您最多可以選擇 50 個儲存貯體。

1. 針對 **Metrics selection** (指標選擇)，選擇 **Advanced metrics and recommendations** (進階指標和建議)。

   如需有關進階指標和建議定價的詳細資訊，請參閱 [Amazon S3 定價](https://aws.amazon.com/s3/pricing/)。

1. 在 **Advanced metrics and recommendations features** (進階指標和建議功能) 下，選取您想要啟用的選項：
   + **Advanced metrics** (進階指標) 
   + **CloudWatch 發佈**
**重要**  
如果為您的 S3 Storage Lens 組態啟用字首彙總，則字首層級指標將不會發佈至 CloudWatch。僅儲存貯體、帳戶和組織層級 S3 Storage Lens 指標發佈至 CloudWatch。
   + **字首彙總**
**注意**  
如需進階指標和建議功能的詳細資訊，請參閱 [指標選擇](storage_lens_basics_metrics_recommendations.md#storage_lens_basics_metrics_selection)。

1. 如果您已啟用 **Advanced metrics** (進階指標)，請選取要在 S3 Storage Lens 儀表板中顯示的 **Advanced metrics categories** (進階指標類別)：
   + **活動指標**
   + **Detailed status code metrics** (詳細的狀態碼指標)
   + **Advanced cost optimization metrics** (進階成本最佳化指標)
   + **Advanced data protection metrics** (進階資料保護指標)

   如需指標類別的詳細資訊，請參閱 [指標類別](storage_lens_basics_metrics_recommendations.md#storage_lens_basics_metrics_types)。如需指標的完整清單，請參閱[Amazon S3 Storage Lens 指標詞彙表](storage_lens_metrics_glossary.md)。

1. (選用) 設定您的指標匯出。

   如需如何設定指標匯出的詳細資訊，請參閱步驟 [使用 S3 主控台](storage_lens_creating_dashboard.md#storage_lens_console_creating)。

1. 選擇 **Create dashboard (建立儀表板)**。

## 使用 AWS CLI
<a name="storage-lens-cloudwatch-enable-publish-cli"></a>

下列 AWS CLI 範例使用 S3 Storage Lens 組織層級進階指標和建議組態來啟用 CloudWatch 發佈選項。若要使用此範例，請以您自己的資訊取代 `user input placeholders`。

```
aws s3control put-storage-lens-configuration --account-id=555555555555 --config-id=your-configuration-id --region=us-east-1 --storage-lens-configuration=file://./config.json

config.json
{
  "Id": "SampleS3StorageLensConfiguration",
  "AwsOrg": {
    "Arn": "arn:aws:organizations::123456789012:organization/o-abcdefgh"
  },
  "AccountLevel": {
    "ActivityMetrics": {
      "IsEnabled":true
    },
    "AdvancedCostOptimizationMetrics": {
      "IsEnabled":true
    },
    "AdvancedDataProtectionMetrics": {
      "IsEnabled":true
    },
    "DetailedStatusCodesMetrics": {
      "IsEnabled":true
    },
    "BucketLevel": {
      "ActivityMetrics": {
        "IsEnabled":true
      },
      "AdvancedCostOptimizationMetrics": {
        "IsEnabled":true
      },
      "DetailedStatusCodesMetrics": {
        "IsEnabled":true
      },
      "PrefixLevel":{
        "StorageMetrics":{
          "IsEnabled":true,
          "SelectionCriteria":{
            "MaxDepth":5,
            "MinStorageBytesPercentage":1.25,
            "Delimiter":"/"
          }
        }
      }
    }
  },
  "Exclude": {
    "Regions": [
      "eu-west-1"
    ],
    "Buckets": [
      "arn:aws:s3:::amzn-s3-demo-source-bucket "
    ]
  },
  "IsEnabled": true,
  "DataExport": {
    "S3BucketDestination": {
      "OutputSchemaVersion": "V_1",
      "Format": "CSV",
      "AccountId": "111122223333",
      "Arn": "arn:aws:s3:::amzn-s3-demo-destination-bucket",
      "Prefix": "prefix-for-your-export-destination",
      "Encryption": {
        "SSES3": {}
      }
    },
    "CloudWatchMetrics": {
      "IsEnabled": true
    }
  }
}
```

## 使用適用於 Java 的 AWS 開發套件
<a name="storage-lens-cloudwatch-enable-publish-sdk"></a>

```
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.ActivityMetrics;
import com.amazonaws.services.s3control.model.BucketLevel;
import com.amazonaws.services.s3control.model.CloudWatchMetrics;
import com.amazonaws.services.s3control.model.Format;
import com.amazonaws.services.s3control.model.Include;
import com.amazonaws.services.s3control.model.OutputSchemaVersion;
import com.amazonaws.services.s3control.model.PrefixLevel;
import com.amazonaws.services.s3control.model.PrefixLevelStorageMetrics;
import com.amazonaws.services.s3control.model.PutStorageLensConfigurationRequest;
import com.amazonaws.services.s3control.model.S3BucketDestination;
import com.amazonaws.services.s3control.model.SSES3;
import com.amazonaws.services.s3control.model.SelectionCriteria;
import com.amazonaws.services.s3control.model.StorageLensAwsOrg;
import com.amazonaws.services.s3control.model.StorageLensConfiguration;
import com.amazonaws.services.s3control.model.StorageLensDataExport;
import com.amazonaws.services.s3control.model.StorageLensDataExportEncryption;
import com.amazonaws.services.s3control.model.StorageLensTag;

import java.util.Arrays;
import java.util.List;

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

public class CreateAndUpdateDashboard {

    public static void main(String[] args) {
        String configurationId = "ConfigurationId";
        String sourceAccountId = "Source Account ID";
        String exportAccountId = "Destination Account ID";
        String exportBucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket"; // The destination bucket for your metrics export must be in the same Region as your S3 Storage Lens configuration.
        String awsOrgARN = "arn:aws:organizations::123456789012:organization/o-abcdefgh";
        Format exportFormat = Format.CSV;

        try {
            SelectionCriteria selectionCriteria = new SelectionCriteria()
                    .withDelimiter("/")
                    .withMaxDepth(5)
                    .withMinStorageBytesPercentage(10.0);
            PrefixLevelStorageMetrics prefixStorageMetrics = new PrefixLevelStorageMetrics()
                    .withIsEnabled(true)
                    .withSelectionCriteria(selectionCriteria);
            BucketLevel bucketLevel = new BucketLevel()
                    .withActivityMetrics(new ActivityMetrics().withIsEnabled(true))
                    .withAdvancedCostOptimizationMetrics(new AdvancedCostOptimizationMetrics().withIsEnabled(true))
                    .withAdvancedDataProtectionMetrics(new AdvancedDataProtectionMetrics().withIsEnabled(true))
                    .withDetailedStatusCodesMetrics(new DetailedStatusCodesMetrics().withIsEnabled(true))
                    .withPrefixLevel(new PrefixLevel().withStorageMetrics(prefixStorageMetrics));
            AccountLevel accountLevel = new AccountLevel()
                    .withActivityMetrics(new ActivityMetrics().withIsEnabled(true))
                    .withAdvancedCostOptimizationMetrics(new AdvancedCostOptimizationMetrics().withIsEnabled(true))
                    .withAdvancedDataProtectionMetrics(new AdvancedDataProtectionMetrics().withIsEnabled(true))
                    .withDetailedStatusCodesMetrics(new DetailedStatusCodesMetrics().withIsEnabled(true))
                    .withBucketLevel(bucketLevel);

            Include include = new Include()
                    .withBuckets(Arrays.asList("arn:aws:s3:::amzn-s3-demo-bucket"))
                    .withRegions(Arrays.asList("us-west-2"));

            StorageLensDataExportEncryption exportEncryption = new StorageLensDataExportEncryption()
                    .withSSES3(new SSES3());
            S3BucketDestination s3BucketDestination = new S3BucketDestination()
                    .withAccountId(exportAccountId)
                    .withArn(exportBucketArn)
                    .withEncryption(exportEncryption)
                    .withFormat(exportFormat)
                    .withOutputSchemaVersion(OutputSchemaVersion.V_1)
                    .withPrefix("Prefix");
            CloudWatchMetrics cloudWatchMetrics = new CloudWatchMetrics()
                    .withIsEnabled(true);
            StorageLensDataExport dataExport = new StorageLensDataExport()
                    .withCloudWatchMetrics(cloudWatchMetrics)
                    .withS3BucketDestination(s3BucketDestination);

            StorageLensAwsOrg awsOrg = new StorageLensAwsOrg()
                    .withArn(awsOrgARN);

            StorageLensConfiguration configuration = new StorageLensConfiguration()
                    .withId(configurationId)
                    .withAccountLevel(accountLevel)
                    .withInclude(include)
                    .withDataExport(dataExport)
                    .withAwsOrg(awsOrg)
                    .withIsEnabled(true);

            List<StorageLensTag> tags = Arrays.asList(
                    new StorageLensTag().withKey("key-1").withValue("value-1"),
                    new StorageLensTag().withKey("key-2").withValue("value-2")
            );

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

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

## 使用 REST API
<a name="storage-lens-cloudwatch-enable-publish-api"></a>

若要使用 Amazon S3 REST API 來啟用 CloudWatch 發佈選項，您可以使用 [https://docs.aws.amazon.com/AmazonS3/latest/API/API_control_PutStorageLensConfiguration.html](https://docs.aws.amazon.com/AmazonS3/latest/API/API_control_PutStorageLensConfiguration.html)。

**後續步驟**  
啟用 CloudWatch 發佈選項後，您可以在 CloudWatch 中存取您的 S3 Storage Lens 指標。您也可以利用 CloudWatch 功能，在 CloudWatch 中監控和分析您的 S3 Storage Lens 資料。如需詳細資訊，請參閱下列主題：
+ [S3 Storage Lens 指標和維度](storage-lens-cloudwatch-metrics-dimensions.md)
+ [使用 CloudWatch 中的 S3 Storage Lens 指標](storage-lens-cloudwatch-monitoring-cloudwatch.md)