Terraform 和 S3 生命周期:如何使特定对象过期?

Terraform & S3 lifecycle: How to expire specific objects?

我正在编写这个 S3 存储桶生命周期规则:

 lifecycle_rules = [
    {
      id      = "log"
      enabled = true
      prefix  = "log/"

      tags = {
        "rule"      = "log"
        "autoclean" = "true"
      }
      expiration = {
        days = 365
      }
    }
  ]

桶的ASCII模拟图如下: 日志每天都动态写入这个桶。一些在文件夹 logs/ 中,一些在子文件夹 failed_logs/ 中,一些在存储桶的根文件夹中。

BUCKET/
|
  nonlogfile.exe
  log_file_that_i_DONT_wanna_delete.log
  logs/ 
      |
       file1     (Created 2021-01-01)
       file2     (Created 2021-01-02)
       failed_logs/
                  |
                   file3 (Created 2021-01-03) 
                   file4 (Created 2021-01-04)

我的目标是让 log/ 文件夹(及其子文件夹 failed_logs 中的每个文件在 365 天后过期。

`file1` would expire at 2022-01-01
`file2` would expire at 2022-01-02
`file3` would expire at 2022-01-03
`file4` would expire at 2022-01-04

nonlogfile.exe
log_file_that_i_DONT_wanna_delete.log

永不过期。

Terraform 的 aws_s3_bucket documentation 指出:

The lifecycle_rule object supports the following:

prefix - (Optional) Object key prefix identifying one or more objects to which the rule applies.

tags - (Optional) Specifies object tags key and value.

如何根据它们在存储桶中的位置,如上所述定位它们?

S3 结构说明

前缀属性只是您的 s3 对象的路径减去存储桶名称。如果您有以下 S3“目录”s3://ec-integration-files/logs/,则前缀值将简单地为 logs/.

该属性被命名为 prefix 而不是 path 因为虽然 s3 在技术上看起来像目录树结构,但它是平面存储。然而,管理控制台显示对象就像它们存储在目录结构中一样,并且它使用前缀来执行此操作。

One other point of clarification, you can have two objects at the root ( or at any depth ) named logs and logs/. One of these will be displayed as a directory in the management console and the other as a file object, but they are really just two objects with different names