skip to Main Content

I want to apply S3 LifecycleConfiguration from .net SDK for below criteria

  1. Prefix, e.g. "tempdocs/"
  2. Tag with its value, e.g { "OneDayExpiry" : "true" }

I’m referring this documentation : https://docs.aws.amazon.com/AmazonS3/latest/userguide/how-to-set-lifecycle-configuration-intro.html

I can’t find option for applying tag by LifecycleTagPredicate, which can consider Tag for LifeCycleRule to delete S3 files

Here is sample rule, where I can only apply expiry days and prefix, but can’t find property for Tags

var lifeCycleConfiguration = new LifecycleConfiguration()
{
    Rules = new List<LifecycleRule>
    {
        new LifecycleRule
        {
             Id = "Delete one day old objects",
             Filter = new LifecycleFilter()
             {
                 LifecycleFilterPredicate = new LifecyclePrefixPredicate()
                 {
                     Prefix = "tempdocs/"
                 }
             },
             Status = LifecycleRuleStatus.Enabled,
             Expiration = new LifecycleRuleExpiration()
             {
                   Days = 1
             }
        }
    }
};

I can see tag property as LifecycleTagPredicate in Java and node SDK as below, but can’t find this in .Net SDK

JAVA SDK

BucketLifecycleConfiguration.Rule rule2 = new BucketLifecycleConfiguration.Rule()
            .withId("Archive and then delete rule")
            .withFilter(new LifecycleFilter(new LifecycleTagPredicate(new Tag("archive", "true"))))
            .addTransition(new Transition().withDays(30).withStorageClass(StorageClass.StandardInfrequentAccess))
            .addTransition(new Transition().withDays(365).withStorageClass(StorageClass.Glacier))
            .withExpirationInDays(3650)
            .withStatus(BucketLifecycleConfiguration.ENABLED);

Do we have any way to create Rule for specific tag in .Net Core SDK?

2

Answers


  1. Chosen as BEST ANSWER

    AWS documentation for .net https://docs.aws.amazon.com/AmazonS3/latest/userguide/how-to-set-lifecycle-configuration-intro.html does not have updated example for LifeCycle rule, and certain properties are deprecated.

    After looking at AWS SDK code for .net and referring to @rlhagerm's answer, Found that as part of Visitor pattern implementation of LifeCycleRule, LifecycleFilterPredicate can take multiple values like Prefix and Tag, where both will be part of same Operands for given Rule

    by this we can create single rule which can suffice both Prefix and Tag to create policy to auto delete files.

    var rule = new LifecycleRule
    {
        Id = "FileExpiryDaysRule",
        Filter = new LifecycleFilter()
        {
            LifecycleFilterPredicate = new LifecycleAndOperator
            {
                Operands = new List<LifecycleFilterPredicate>
                {
                    new LifecyclePrefixPredicate()
                    {
                        Prefix = "tempdocs/"
                    },
                    new LifecycleTagPredicate()
                    {
                        Tag = new Amazon.S3.Model.Tag { Key = "OneDayExpiry", Value = "true" },
                    }
                }
            }
        },
        Status = LifecycleRuleStatus.Enabled,
        Expiration = new LifecycleRuleExpiration() { Days = 1 },
    };
    
    var bucketLifecycleConfiguration = new LifecycleConfiguration
    {
        Rules = new List<LifecycleRule>
        {
            { rule }
        }
    };
    
    var putRequest = new PutLifecycleConfigurationRequest
    {
        BucketName = "yourBucketName",
        Configuration = bucketLifecycleConfiguration
    };
    
    var policy = await client.PutLifecycleConfigurationAsync(putRequest);
    

  2. You can set the LifeCycleFilterPredicate to be a new LifecycleTagPredicate to use tags. If you want another rule for prefix also, you can add multiple rules to the Rules collection. There are some good examples here with multiple rules in XML, but they may still be helpful in understanding the structure.

    var lifeCycleConfiguration = new LifecycleConfiguration()
        {
            Rules = new List<LifecycleRule>
            {
                new LifecycleRule
                {
                    Id = "Delete one day old objects",
                    Filter = new LifecycleFilter()
                    {
                        LifecycleFilterPredicate = new LifecycleTagPredicate()
                        {
                            Tag = new Amazon.S3.Model.Tag(){Key = "OneDayExpiry", Value = "true"}
                        }
    
                    },
                    Status = LifecycleRuleStatus.Enabled,
                    Expiration = new LifecycleRuleExpiration()
                    {
                        Days = 1
                    }
                }
            }
        };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search