Menu
Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Manage an Object's Lifecycle Using the AWS SDK for .NET

You can use the AWS SDK for .NET to manage the lifecycle configuration on a bucket. For more information about managing lifecycle configuration, see Object Lifecycle Management.

Note

When you add a lifecycle configuration, Amazon S3 replaces the existing lifecycle configuration on the specified bucket. To update a configuration, you must first retrieve the lifecycle configuration, make the changes, and then add the revised lifecycle configuration to the bucket.

Example .NET Code Example

The following example shows how to use the AWS SDK for .NET to add, update, and delete a bucket's lifecycle configuration. The code example does the following:

  • Adds a lifecycle configuration to a bucket.

  • Retrieves the lifecyle configuration and updates it by adding another rule.

  • Adds the modified lifecycle configuration to the bucket. Amazon S3 replaces the existing lifecycle configuration.

  • Retrieves the configuration again and verifies it by printing the number of rules in the configuration.

  • Deletes the lifecyle configuration.and verifies the deletion

For instructions on how to create and test a working sample, see Running the Amazon S3 .NET Code Examples.

// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.) 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 = "*** bucket name ***"; // 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 = "Archive immediately rule", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate() { Prefix = "glacierobjects/" } }, Status = LifecycleRuleStatus.Enabled, Transitions = new List<LifecycleTransition> { new LifecycleTransition { Days = 0, StorageClass = S3StorageClass.Glacier } }, }, new LifecycleRule { Id = "Archive and then delete rule", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate() { Prefix = "projectdocs/" } }, Status = LifecycleRuleStatus.Enabled, Transitions = new List<LifecycleTransition> { new LifecycleTransition { Days = 30, StorageClass = S3StorageClass.StandardInfrequentAccess }, new LifecycleTransition { Days = 365, StorageClass = S3StorageClass.Glacier } }, Expiration = new LifecycleRuleExpiration() { Days = 3650 } } } }; // 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 = "NewRule", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate() { Prefix = "YearlyDocuments/" } }, Expiration = new LifecycleRuleExpiration() { Days = 3650 } }); // Add the configuration to the bucket. await AddExampleLifecycleConfigAsync(client, lifeCycleConfiguration); // Verify that there are now three rules. lifeCycleConfiguration = await RetrieveLifecycleConfigAsync(client); Console.WriteLine("Expected # of rulest=3; 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); } } }