使用 AWS CLI 和适用于 Java 的 SDK 创建和管理生命周期配置
您可以使用 S3 生命周期优化 Amazon S3 on Outposts 的存储容量。您可以创建生命周期规则,使对象在老化时过期或被较新版本取代。您可以创建、启用、禁用或删除生命周期规则。
有关 S3 生命周期的更多信息,请参阅为 Amazon S3 on Outposts 桶创建和管理生命周期配置。
注意
创建桶的 AWS 账户拥有该桶,并且是唯一可以创建、启用、禁用或删除生命周期规则的账户。
要使用 AWS Command Line Interface(AWS CLI)和 AWS SDK for Java 创建和管理 S3 on Outposts 桶的生命周期配置,请参阅以下示例。
放置 (PUT) 生命周期配置
- AWS CLI
以下 AWS CLI 示例在 Outposts 桶上放置生命周期配置策略。此策略指定具有标记前缀 (
) 的所有对象,并且标签在 10 天后过期。要使用此示例,请将每个myprefix
替换为您自己的信息。user input placeholder
-
将生命周期配置策略保存到 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
} } ] } -
将 JSON 文件作为
put-bucket-lifecycle-configuration
CLI 命令的一部分提交。要使用此命令,请将每个
替换为您自己的信息。有关此命令的更多信息,请参阅《AWS CLI 参考》中的 put-bucket-lifecycle-configurationuser input placeholder
。 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 的 SDK 示例在 Outposts 桶上放置生命周期配置。此生命周期配置指定具有标记前缀 (
) 的所有对象,并且标签在 10 天后过期。要使用此示例,请将每个myprefix
替换为您自己的信息。有关更多信息,请参阅《Amazon Simple Storage Service API 参考》中的 PutBucketLifecycleConfiguration。user input placeholder
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()); }
获取(GET)S3 on Outposts 桶上的生命周期配置
- AWS CLI
-
以下 AWS CLI 示例获取 Outposts 桶上的生命周期配置。要使用此命令,请将每个
替换为您自己的信息。有关此命令的更多信息,请参阅《AWS CLI 参考》中的 get-bucket-lifecycle-configurationuser input placeholder
。 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 的 SDK 示例获取 Outposts 桶的生命周期配置。有关更多信息,请参阅《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()); }