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

Manage Object Lifecycle Using the AWS SDK for .NET

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

Example .NET Code Example

The following C# code example adds lifecycle configuration to a bucket. The example shows two lifecycle configurations:

  • Lifecycle configuration that uses only prefix to select a subset of objects to which the rule applies.

  • Lifecycle configuration that uses a prefix and object tags to select a subset of objects to which the rule applies.

The lifecycle rule transitions objects to the GLACIER storage class soon after the objects are created.

Note

The following code works with the latest version of the .NET SDK.

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

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Amazon.S3;
using Amazon.S3.Model;

namespace aws.amazon.com.s3.documentation
{
    class LifeCycleTest
    {
        static string bucketName = "*** bucket name ***";

        public static void Main(string[] args)
        {
            try
            {
                using (var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
                {
                    // 1. Add lifecycle config with prefix only.
                    var lifeCycleConfigurationA = LifecycleConfig1();

                    // Add the configuration to the bucket 
                    PutLifeCycleConfiguration(client, lifeCycleConfigurationA);

                    // Retrieve an existing configuration 
                    var lifeCycleConfiguration = GetLifeCycleConfiguration(client);


                    // 2. Add lifecycle config with prefix and tags.
                    var lifeCycleConfigurationB = LifecycleConfig2();

                    // Add the configuration to the bucket 
                    PutLifeCycleConfiguration(client, lifeCycleConfigurationB);

                    // Retrieve an existing configuration 
                    lifeCycleConfiguration = GetLifeCycleConfiguration(client);

                    // 3. Delete lifecycle config.
                    DeleteLifecycleConfiguration(client);

                    // 4. Retrieve a nonexistent configuration
                    lifeCycleConfiguration = GetLifeCycleConfiguration(client);
                    Debug.Assert(lifeCycleConfiguration == null);
                }

                Console.WriteLine("Example complete. To continue, click Enter...");
                Console.ReadKey();
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
            }
        }

        private static LifecycleConfiguration LifecycleConfig1()
        {
            var lifeCycleConfiguration = new LifecycleConfiguration()
            {
                Rules = new List<LifecycleRule>
                        {
                             new LifecycleRule
                            {
                                Id = "Rule-1",
                                Filter = new LifecycleFilter()
                                {
                                    LifecycleFilterPredicate = new LifecyclePrefixPredicate
                                    { 
                                        Prefix = "glacier/"
                                    }
                                },
                                Status = LifecycleRuleStatus.Enabled,
                                Transitions = new List<LifecycleTransition>
                                {
                                    new LifecycleTransition
                                    {
                                        Days = 0,
                                        StorageClass = S3StorageClass.Glacier
                                    }
                                },
                            }
                        }
            };
            return lifeCycleConfiguration;
        }

        private static LifecycleConfiguration LifecycleConfig2()
        {
            var lifeCycleConfiguration = new LifecycleConfiguration()
            {
                Rules = new List<LifecycleRule>
                        {
                             new LifecycleRule
                            {
                                Id = "Rule-1",
                                Filter = new LifecycleFilter()
                                {
                                    LifecycleFilterPredicate  = new LifecycleAndOperator
                                    {
                                        Operands = new List<LifecycleFilterPredicate>
                                        {
                                            new LifecyclePrefixPredicate
                                            {
                                                Prefix = "glacierobjects/"
                                            },
                                            new LifecycleTagPredicate
                                            {
                                                Tag = new Tag()
                                                {
                                                    Key = "tagKey1",
                                                    Value = "tagValue1"
                                                }
                                            },
                                             new LifecycleTagPredicate
                                            {
                                                Tag = new Tag()
                                                {
                                                    Key = "tagKey2",
                                                    Value = "tagValue2"
                                                }
                                            }
                                        }
                                    }
                                },
                                Status = LifecycleRuleStatus.Enabled,
                                Transitions = new List<LifecycleTransition>
                                                 {
                                                      new LifecycleTransition
                                                      {
                                                           Days = 0,
                                                           StorageClass = S3StorageClass.Glacier
                                                      }
                                                  },
                            }
                        }
            };
            return lifeCycleConfiguration;
        }

        static void PutLifeCycleConfiguration(IAmazonS3 client, LifecycleConfiguration configuration)
        {

            PutLifecycleConfigurationRequest request = new PutLifecycleConfigurationRequest
            {
                BucketName = bucketName,
                Configuration = configuration
            };

            var response = client.PutLifecycleConfiguration(request);
        }

        static LifecycleConfiguration GetLifeCycleConfiguration(IAmazonS3 client)
        {
            GetLifecycleConfigurationRequest request = new GetLifecycleConfigurationRequest
            {
                BucketName = bucketName

            };
            var response = client.GetLifecycleConfiguration(request);
            var configuration = response.Configuration;
            return configuration;
        }

        static void DeleteLifecycleConfiguration(IAmazonS3 client)
        {
            DeleteLifecycleConfigurationRequest request = new DeleteLifecycleConfigurationRequest
            {
                BucketName = bucketName
            };
            client.DeleteLifecycleConfiguration(request);
        }
    }
}