amazon-web-servicesamazon-s3.net-cores3-lifecycle-policy

Can't find S3 LifecycleTagPredicate in .net sdk for tag based configuration


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?


Solution

  • 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);