為您的目錄儲存貯體建立和管理生命週期組態 - Amazon Simple Storage Service

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

為您的目錄儲存貯體建立和管理生命週期組態

您可以使用 (AWS CLI)、 AWS SDKs 和 REST 為目錄儲存貯 AWS Command Line Interface 體建立生命週期組態APIs。

您可以使用下列 AWS CLI 命令來管理 S3 生命週期組態:

  • put-bucket-lifecycle-configuration

  • get-bucket-lifecycle-configuration

  • delete-bucket-lifecycle

如需設定 的指示 AWS CLI,請參閱 Amazon S3 Word 參考中的使用 AWS CLI 與 Amazon S3 一起開發Amazon S3 API

Amazon S3 生命週期組態是 XML 檔案。但是,當您使用 時 AWS CLI,您無法指定 XML 格式。您必須改為指定 JSON 格式。以下是 XML 生命週期組態範例,以及您可以在 AWS CLI命令中指定的對等 JSON 組態。

下列 AWS CLI 範例會在目錄儲存貯體上放置生命週期組態政策。此政策會指定所有具有已標記字首 (myprefix) 且為定義物件大小的物件,都會在 7 天後過期。若要使用此範例,請取代每個 user input placeholder 使用您自己的資訊。

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

JSON
{ "Rules": [ { "Expiration": { "Days": 7 }, "ID": "Lifecycle expiration rule", "Filter": { "And": { "Prefix": "myprefix/", "ObjectSizeGreaterThan": 500, "ObjectSizeLessThan": 64000 } }, "Status": "Enabled" } ] }

提交 JSON 檔案作為 CLI put-bucket-lifecycle-configuration 命令的一部分。若要使用此命令,請取代每個 user input placeholder 使用您自己的資訊。

aws s3api put-bucket-lifecycle-configuration --region us-west-2 --profile default --bucket amzn-s3-demo-bucket--usw2-az1--x-s3 --lifecycle-configuration file://lc-policy.json --checksum-algorithm crc32c
XML
<LifecycleConfiguration> <Rule> <ID>Lifecycle expiration rule</ID> <Filter> <And> <Prefix>myprefix/</Prefix> <ObjectSizeGreaterThan>500</ObjectSizeGreaterThan> <ObjectSizeLessThan>64000</ObjectSizeLessThan> </And> </Filter> <Status>Enabled</Status> <Expiration> <Days>7</Days> </Expiration> </Rule> </LifecycleConfiguration>
SDK for Java
import software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest; import software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationResponse; import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm; import software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration; import software.amazon.awssdk.services.s3.model.LifecycleRule; import software.amazon.awssdk.services.s3.model.LifecycleRuleFilter; import software.amazon.awssdk.services.s3.model.LifecycleExpiration; import software.amazon.awssdk.services.s3.model.LifecycleRuleAndOperator; import software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationResponse; import software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationRequest; import software.amazon.awssdk.services.s3.model.DeleteBucketLifecycleRequest; import software.amazon.awssdk.services.s3.model.DeleteBucketLifecycleResponse; import software.amazon.awssdk.services.s3.model.AbortIncompleteMultipartUpload; // PUT a Lifecycle policy LifecycleRuleFilter objectExpirationFilter = LifecycleRuleFilter.builder().and(LifecycleRuleAndOperator.builder().prefix("dir1/").objectSizeGreaterThan(3L).objectSizeLessThan(20L).build()).build(); LifecycleRuleFilter mpuExpirationFilter = LifecycleRuleFilter.builder().prefix("dir2/").build(); LifecycleRule objectExpirationRule = LifecycleRule.builder().id("lc").filter(objectExpirationFilter).status("Enabled").expiration(LifecycleExpiration.builder() .days(10) .build()) .build(); LifecycleRule mpuExpirationRule = LifecycleRule.builder().id("lc-mpu").filter(mpuExpirationFilter).status("Enabled").abortIncompleteMultipartUpload(AbortIncompleteMultipartUpload.builder() .daysAfterInitiation(10) .build()) .build(); PutBucketLifecycleConfigurationRequest putLifecycleRequest = PutBucketLifecycleConfigurationRequest.builder() .bucket("amzn-s3-demo-bucket--usw2-az1--x-s3") .checksumAlgorithm(ChecksumAlgorithm.CRC32) .lifecycleConfiguration( BucketLifecycleConfiguration.builder() .rules(objectExpirationRule, mpuExpirationRule) .build() ).build(); PutBucketLifecycleConfigurationResponse resp = client.putBucketLifecycleConfiguration(putLifecycleRequest); // GET the Lifecycle policy GetBucketLifecycleConfigurationResponse getResp = client.getBucketLifecycleConfiguration(GetBucketLifecycleConfigurationRequest.builder().bucket("amzn-s3-demo-bucket--usw2-az1--x-s3").build()); // DELETE the Lifecycle policy DeleteBucketLifecycleResponse delResp = client.deleteBucketLifecycle(DeleteBucketLifecycleRequest.builder().bucket("amzn-s3-demo-bucket--usw2-az1--x-s3").build());
SDK for Go
package main import ( "context" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" ) // PUT a Lifecycle policy func putBucketLifecycleConfiguration(client *s3.Client, bucketName string ) error { lifecycleConfig := &s3.PutBucketLifecycleConfigurationInput{ Bucket: aws.String(bucketName), LifecycleConfiguration: &types.BucketLifecycleConfiguration{ Rules: []types.LifecycleRule{ { ID: aws.String("lc"), Filter: &types.LifecycleRuleFilter{ And: &types.LifecycleRuleAndOperator{ Prefix: aws.String("foo/"), ObjectSizeGreaterThan: aws.Int64(1000000), ObjectSizeLessThan: aws.Int64(100000000), }, }, Status: types.ExpirationStatusEnabled, Expiration: &types.LifecycleExpiration{ Days: aws.Int32(int32(1)), }, }, { ID: aws.String("abortmpu"), Filter: &types.LifecycleRuleFilter{ Prefix: aws.String("bar/"), }, Status: types.ExpirationStatusEnabled, AbortIncompleteMultipartUpload: &types.AbortIncompleteMultipartUpload{ DaysAfterInitiation: aws.Int32(int32(5)), }, }, }, }, } _, err := client.PutBucketLifecycleConfiguration(context.Background(), lifecycleConfig) return err } // Get the Lifecycle policy func getBucketLifecycleConfiguration(client *s3.Client, bucketName string) error { getLifecycleConfig := &s3.GetBucketLifecycleConfigurationInput{ Bucket: aws.String(bucketName), } resp, err := client.GetBucketLifecycleConfiguration(context.Background(), getLifecycleConfig) if err != nil { return err } return nil } // Delete the Lifecycle policy func deleteBucketLifecycleConfiguration(client *s3.Client, bucketName string) error { deleteLifecycleConfig := &s3.DeleteBucketLifecycleInput{ Bucket: aws.String(bucketName), } _, err := client.DeleteBucketLifecycle(context.Background(), deleteLifecycleConfig) return err } func main() { cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion("us-west-2")) // Specify your region here if err != nil { log.Fatalf("unable to load SDK config, %v", err) } s3Client := s3.NewFromConfig(cfg) bucketName := "amzn-s3-demo-bucket--usw2-az1--x-s3" putBucketLifecycleConfiguration(s3Client, bucketName) getBucketLifecycleConfiguration(s3Client, bucketName) deleteBucketLifecycleConfiguration(s3Client, bucketName) getBucketLifecycleConfiguration(s3Client, bucketName) }
SDK for .NET
using Amazon; using Amazon.S3; using Amazon.S3.Model; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class LifecycleTest { private const string bucketName = "amzn-s3-demo-bucket--usw2-az1--x-s3"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 client; public static void Main() { client = new AmazonS3Client(bucketRegion); AddUpdateDeleteLifecycleConfigAsync().Wait(); } private static async Task AddUpdateDeleteLifecycleConfigAsync() { try { var lifeCycleConfiguration = new LifecycleConfiguration() { Rules = new List <LifecycleRule> { new LifecycleRule { Id = "delete rule", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate() { Prefix = "projectdocs/" } }, Status = LifecycleRuleStatus.Enabled, Expiration = new LifecycleRuleExpiration() { Days = 10 } } } }; // Add the configuration to the bucket. await AddExampleLifecycleConfigAsync(client, lifeCycleConfiguration); // Retrieve an existing configuration. lifeCycleConfiguration = await RetrieveLifecycleConfigAsync(client); // Add a new rule. lifeCycleConfiguration.Rules.Add(new LifecycleRule { Id = "mpu abort rule", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate() { Prefix = "YearlyDocuments/" } }, Expiration = new LifecycleRuleExpiration() { Days = 10 }, AbortIncompleteMultipartUpload = new LifecycleRuleAbortIncompleteMultipartUpload() { DaysAfterInitiation = 10 } }); // Add the configuration to the bucket. await AddExampleLifecycleConfigAsync(client, lifeCycleConfiguration); // Verify that there are now two rules. lifeCycleConfiguration = await RetrieveLifecycleConfigAsync(client); Console.WriteLine("Expected # of rulest=2; found:{0}", lifeCycleConfiguration.Rules.Count); // Delete the configuration. await RemoveLifecycleConfigAsync(client); // Retrieve a nonexistent configuration. lifeCycleConfiguration = await RetrieveLifecycleConfigAsync(client); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } } static async Task AddExampleLifecycleConfigAsync(IAmazonS3 client, LifecycleConfiguration configuration) { PutLifecycleConfigurationRequest request = new PutLifecycleConfigurationRequest { BucketName = bucketName, Configuration = configuration }; var response = await client.PutLifecycleConfigurationAsync(request); } static async Task <LifecycleConfiguration> RetrieveLifecycleConfigAsync(IAmazonS3 client) { GetLifecycleConfigurationRequest request = new GetLifecycleConfigurationRequest { BucketName = bucketName }; var response = await client.GetLifecycleConfigurationAsync(request); var configuration = response.Configuration; return configuration; } static async Task RemoveLifecycleConfigAsync(IAmazonS3 client) { DeleteLifecycleConfigurationRequest request = new DeleteLifecycleConfigurationRequest { BucketName = bucketName }; await client.DeleteLifecycleConfigurationAsync(request); } } }
SDK for Python
import boto3 client = boto3.client("s3", region_name="us-west-2") bucket_name = 'amzn-s3-demo-bucket--usw2-az1--x-s3' client.put_bucket_lifecycle_configuration( Bucket=bucket_name, ChecksumAlgorithm='CRC32', LifecycleConfiguration={ 'Rules': [ { 'ID': 'lc', 'Filter': { 'And': { 'Prefix': 'foo/', 'ObjectSizeGreaterThan': 1000000, 'ObjectSizeLessThan': 100000000, } }, 'Status': 'Enabled', 'Expiration': { 'Days': 1 } }, { 'ID': 'abortmpu', 'Filter': { 'Prefix': 'bar/' }, 'Status': 'Enabled', 'AbortIncompleteMultipartUpload': { 'DaysAfterInitiation': 5 } } ] } ) result = client.get_bucket_lifecycle_configuration( Bucket=bucket_name ) client.delete_bucket_lifecycle( Bucket=bucket_name )