Creating and managing a lifecycle configuration by using the AWS CLI and SDK for Java - Amazon S3 on Outposts

Creating and managing a lifecycle configuration by using the AWS CLI and SDK for Java

You can use S3 Lifecycle to optimize storage capacity for Amazon S3 on Outposts. You can create lifecycle rules to expire objects as they age or are replaced by newer versions. You can create, enable, disable, or delete a lifecycle rule.

For more information about S3 Lifecycle, see Creating and managing a lifecycle configuration for your Amazon S3 on Outposts bucket.

Note

The AWS account that creates the bucket owns it and is the only one that can create, enable, disable, or delete a lifecycle rule.

To create and manage a lifecycle configuration for an S3 on Outposts bucket by using the AWS Command Line Interface (AWS CLI) and the AWS SDK for Java, see the following examples.

PUT a lifecycle configuration

AWS CLI

The following AWS CLI example puts a lifecycle configuration policy on an Outposts bucket. This policy specifies that all objects that have the flagged prefix (myprefix) and tags expire after 10 days. To use this example, replace each user input placeholder with your own information.

  1. Save the lifecycle configuration policy to a JSON file. In this example, the file is named 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. Submit the JSON file as part of the put-bucket-lifecycle-configuration CLI command. To use this command, replace each user input placeholder with your own information. For more information about this command, see put-bucket-lifecycle-configuration in the AWS CLI Reference.

    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

The following SDK for Java example puts a lifecycle configuration on an Outposts bucket. This lifecycle configuration specifies that all objects that have the flagged prefix (myprefix) and tags expire after 10 days. To use this example, replace each user input placeholder with your own information. For more information, see PutBucketLifecycleConfiguration in the Amazon Simple Storage Service API Reference.

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 the lifecycle configuration on an S3 on Outposts bucket

AWS CLI

The following AWS CLI example gets a lifecycle configuration on an Outposts bucket. To use this command, replace each user input placeholder with your own information. For more information about this command, see get-bucket-lifecycle-configuration in the AWS CLI Reference.

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

The following SDK for Java example gets a lifecycle configuration for an Outposts bucket. For more information, see GetBucketLifecycleConfiguration in the Amazon Simple Storage Service API Reference.

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()); }