Home > Software engineering >  How can I update a lifecycle configuration with a filter based on both prefix and multiple tags in R
How can I update a lifecycle configuration with a filter based on both prefix and multiple tags in R

Time:10-19

I want to put a lifecycle_configuration to an S3 bucket with a rule that uses a filter with multiple tags and a prefix.

I can successfully put_lifecycle_configuration if the filter uses only one tag or one prefix, but I get a Aws::S3::Errors::MalformedXML (The XML you provided was not well-formed or did not validate against our published schema) response from AWS if I try to use an and: to combine multiple tags or a tag and a prefix.

(edit: put the prefix:... within the and: Hash per Ermiya's answer below)

What am I doing wrong?

Here is my rule:

aws_s3_backup_prefix = "production_backup" # this is fetched from ENV in real life

rule_expire_yearly_after_10y = {
        id: "Expire 1 January backups after 10 years",
        filter: {
          and: {
            prefix: aws_s3_backup_prefix,
            tags: [
              { key: 'date-month-day', value: '1'},
              { key: 'date-month-num', value: '1'}
            ]
          }
        },
        status: 'Enabled',
        expiration: {
          days: 3650
        }
      }

And here is how I use it to put the lifecycle configuration:

# aws_client is a valid Aws::S3::Client
# I have access to aws_s3_backup_bucket_name
# I can get and put a simple lifecycle_configuration (no 'and:') with this client and bucket

aws_client.put_bucket_lifecycle_configuration({
          bucket: aws_s3_backup_bucket_name,
          lifecycle_configuration: {
            rules: [ rule_expire_yearly_after_10y ]
          }
        })

Config:

  • ruby 2.6.6
  • aws-sdk-core 3.109.1
  • aws-sdk-s3 1.103.0

AWS Documentation: S3 User Guide: Examples of lifecycle configuration

CodePudding user response:

To specify a filter based on the key prefix and one or more tags, you need to place the prefix inside of the and element, not outside. Amazon S3 then can combine the prefix and tag filters.

This is why it's complaining about malformed XML.

This should apply the lifecycle rule to objects with a key prefix of aws_s3_backup_prefix, date-month-day tag value of 1 & date-month-num tag value of 1:

rule_expire_yearly_after_10y = {
  id: "Expire 1 January backups after 10 years",
  filter: {
    and: {
      prefix: aws_s3_backup_prefix,
      tags: [
        { key: 'date-month-day', value: '1'},
        { key: 'date-month-num', value: '1'}
      ]
    }
  },
  status: 'Enabled',
  expiration: {
    days: 3650
  }
}

CodePudding user response:

This bug was fixed in the very next version. I was using aws-sdk-core 3.109.1 and this was fixed in aws-sdk-core 3.109.2

  • Related