使用適用於 Java 的 AWS CLI 和 SDK 建立和管理生命週期組態 - Amazon S3 on Outposts

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

使用適用於 Java 的 AWS CLI 和 SDK 建立和管理生命週期組態

您可以使用 S3 生命週期,最佳化 Amazon S3 on Outposts 的儲存容量。您可以建立生命週期規則,在物件老化或取代為較新的版本時使這些物件過期。您可以建立、啟用、停用或刪除生命週期規則。

如需 S3 生命週期的詳細資訊,請參閱 建立和管理 Amazon S3 on Outposts 儲存貯體的生命週期組態

注意

建立儲存貯體 AWS 帳戶 的 擁有它,而且是唯一可以建立、啟用、停用或刪除生命週期規則的 。

若要使用 AWS Command Line Interface (AWS CLI) 和 建立和管理 S3 on Outposts 儲存貯體的生命週期組態 適用於 Java 的 AWS SDK,請參閱下列範例。

放置生命週期組態

AWS CLI

下列 AWS CLI 範例會在 Outposts 儲存貯體上放置生命週期組態政策。此政策指定,所有包含標記字首 (myprefix) 和標籤的物件會在 10 天後過期。若要使用此範例,請以您自己的資訊取代每個 user input placeholder

  1. 將生命週期組態原則儲存至 JSON 檔案。在此範例中,檔案命名為 lifecycle1.json

    { "Rules": [ { "ID": "id-1", "Filter": { "And": { "Prefix": "myprefix", "Tags": [ { "Value": "mytagvalue1", "Key": "mytagkey1" }, { "Value": "mytagvalue2", "Key": "mytagkey2" } ], "ObjectSizeGreaterThan": 1000, "ObjectSizeLessThan": 5000 } }, "Status": "Enabled", "Expiration": { "Days": 10 } } ] }
  2. 提交 JSON 檔案以做為 put-bucket-lifecycle-configuration CLI 命令的一部分。若要執行此命令,請以您自己的資訊取代每個 user input placeholder。如需此命令的詳細資訊,請參閱 AWS CLI 參考中的 put-bucket-lifecycle-configuration

    aws s3control put-bucket-lifecycle-configuration --account-id 123456789012 --bucket arn:aws:s3-outposts:region:123456789012:outpost/op-01ac5d28a6a232904/bucket/example-outposts-bucket --lifecycle-configuration file://lifecycle1.json
SDK for Java

下列適用於 Java 的開發套件範例將生命週期組態放置在 Outpost 儲存貯體上。此生命週期組態指定,所有包含標記字首 (myprefix) 和標籤的物件會在 10 天後過期。若要使用此範例,請以您自己的資訊取代每個 user input placeholder。如需詳細資訊,請參閱 Amazon Simple Storage Service API 參考中的 PutBucketLifecycleConfiguration

import com.amazonaws.services.s3control.model.*; public void putBucketLifecycleConfiguration(String bucketArn) { S3Tag tag1 = new S3Tag().withKey("mytagkey1").withValue("mytagkey1"); S3Tag tag2 = new S3Tag().withKey("mytagkey2").withValue("mytagkey2"); LifecycleRuleFilter lifecycleRuleFilter = new LifecycleRuleFilter() .withAnd(new LifecycleRuleAndOperator() .withPrefix("myprefix") .withTags(tag1, tag2)) .withObjectSizeGreaterThan(1000) .withObjectSizeLessThan(5000); LifecycleExpiration lifecycleExpiration = new LifecycleExpiration() .withExpiredObjectDeleteMarker(false) .withDays(10); LifecycleRule lifecycleRule = new LifecycleRule() .withStatus("Enabled") .withFilter(lifecycleRuleFilter) .withExpiration(lifecycleExpiration) .withID("id-1"); LifecycleConfiguration lifecycleConfiguration = new LifecycleConfiguration() .withRules(lifecycleRule); PutBucketLifecycleConfigurationRequest reqPutBucketLifecycle = new PutBucketLifecycleConfigurationRequest() .withAccountId(AccountId) .withBucket(bucketArn) .withLifecycleConfiguration(lifecycleConfiguration); PutBucketLifecycleConfigurationResult respPutBucketLifecycle = s3ControlClient.putBucketLifecycleConfiguration(reqPutBucketLifecycle); System.out.printf("PutBucketLifecycleConfiguration Response: %s%n", respPutBucketLifecycle.toString()); }

取得 S3 on Outposts 儲存貯體的生命週期組態

AWS CLI

下列 AWS CLI 範例取得 Outposts 儲存貯體的生命週期組態。若要執行此命令,請以您自己的資訊取代每個 user input placeholder。如需此命令的詳細資訊,請參閱 AWS CLI 參考中的 get-bucket-lifecycle-configuration

aws s3control get-bucket-lifecycle-configuration --account-id 123456789012 --bucket arn:aws:s3-outposts:<your-region>:123456789012:outpost/op-01ac5d28a6a232904/bucket/example-outposts-bucket
SDK for Java

下列適用於 Java 的開發套件範例取得 Outpost 儲存貯體的生命週期組態。如需詳細資訊,請參閱 Amazon Simple Storage Service API 參考中的 GetBucketLifecycleConfiguration

import com.amazonaws.services.s3control.model.*; public void getBucketLifecycleConfiguration(String bucketArn) { GetBucketLifecycleConfigurationRequest reqGetBucketLifecycle = new GetBucketLifecycleConfigurationRequest() .withAccountId(AccountId) .withBucket(bucketArn); GetBucketLifecycleConfigurationResult respGetBucketLifecycle = s3ControlClient.getBucketLifecycleConfiguration(reqGetBucketLifecycle); System.out.printf("GetBucketLifecycleConfiguration Response: %s%n", respGetBucketLifecycle.toString()); }