如何使用基于 Ruby 中的前缀和多个标签的过滤器更新生命周期配置?

How can I update a lifecycle configuration with a filter based on both prefix and multiple tags in Ruby?

我想将 lifecycle_configuration 放入 S3 存储桶,其规则使用具有 多个标签 前缀[=44 的过滤器=].

如果过滤器只使用一个标签或一个前缀,我可以成功 put_lifecycle_configuration,但如果我尝试使用 and: 组合多个,我会从 AWS 得到 Aws::S3::Errors::MalformedXML (The XML you provided was not well-formed or did not validate against our published schema) 响应标签或标签和前缀。

(编辑:将 prefix:... 放入下面每个 答案的 and: 哈希中)

我做错了什么?

这是我的规则:

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
        }
      }

下面是我如何使用它来放置生命周期配置:

# 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 ]
          }
        })

配置:

  • ruby2.6.6
  • aws-sdk-核心 3.109.1
  • aws-sdk-s3 1.103.0

AWS Documentation: S3 User Guide: Examples of lifecycle configuration

要根据键前缀 一个或多个标记指定过滤器,您需要放置 prefix inside of the and element,而不是 外面。然后 Amazon S3 可以组合前缀和标签过滤器。

这就是它抱怨格式错误的原因 XML。

这应该将生命周期规则应用于键前缀为 aws_s3_backup_prefixdate-month-day 标记值为 1 和 date-month-num 标记值为 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
  }
}

此错误已在下一版本中修复。 我使用的是 aws-sdk-core 3.109.1,这是 fixed in aws-sdk-core 3.109.2