AWS S3 生命周期规则

huangapple go评论82阅读模式
英文:

AWS S3 Lifecyclerules

问题

我已经尝试了几天来设置AWS S3上的版本控制。
设置版本控制本身很简单,但我想避免永久性地为我的对象创建版本。
我看到这需要使用生命周期钩子。但是它们每天只运行一次,我无法手动运行它们,这使得迭代速度很慢。

我想要做的是:

  • 对所有对象进行版本控制,并保留版本,包括可能的删除标记,保留15天。
  • 除了health-* 开头的对象,我不想创建版本(如果不可能,仅保留1天的版本),我也不想保留删除标记。

此外,我的对象以每个客户端的路径存储(/client/app/myFile,/client/app/health-xxx)。我不知道这是否会影响前缀规则?

我尝试过自己,参考了这个文档的帮助,但我很难将我的高级业务需求转化为具体的规则。而且正如我所说,由于我只能每天迭代一次,所以速度很慢^^。

英文:

I've been trying for a few days to set up versioning on AWS S3.
Setting up the versioning itself was simple, but I would like to avoid versioning my objects forever.
I saw that this required the use of LifecycleHooks. Except that they only run once a day, and I can't run them manually, which makes iterations slow.

What I would like to do is:

  • Version all objects, and keep the versions, including potential deletion markers, for 15 days.
  • Except the objects which starts by health-* that I don't want to version (if not possible, only keep versions during 1 day), and I don't want to keep deletion markers either.

Moreover, my objects are stored with a path per client (/client/app/myFile, /client/app/health-xxx). I don't know if it has an impact on the rules prefix?

I tried by myself, with the help of this documentation, but I experiment difficulties to translate my high level business need to concrete rules. And like I said, since I can iterate only once a day, it's quite slow ^^'

答案1

得分: 1

首先要明确的是,你不能只为某些对象启用版本控制而排除其他对象。要么你启用,要么你不启用。

> 当你在一个存储桶中启用版本控制时,所有新对象都会被赋予一个唯一的版本ID并进行版本控制。在启用版本控制时,存储桶中已经存在的对象将以后始终具有版本控制。

另外,请记住:

> 在设置版本控制状态之前存储在存储桶中的对象的版本ID为空。启用版本控制后,存储桶中现有的对象不会改变。

说到这里,你可能实际上需要多于一个规则,但我不确定,因为你的用例不清楚。

无论如何,在一个存储桶中使用多个规则时,请记住:

> 当在S3生命周期配置中有多个规则时,一个对象可以同时符合多个S3生命周期操作的条件。在这种情况下,Amazon S3遵循以下一般规则:

  • 永久删除优先于转换。

  • 转换优先于创建删除标记。

所以,总结一下,你可以使用类似以下的配置:

{
  "Rules": [
    {
      "ID": "Delete-Objects-After-15-Days",
      "Filter": {
        "And": {
          "Prefix": "",
          "Tags": [],
          "Not": {
            "Prefix": "PATH/TO/YOUR/OBJECTS/WITH/PREFIX/health-"
          }
        }
      },
      "Status": "Enabled",
      "Transitions": [],
      "NoncurrentVersionTransitions": [],
      "NoncurrentVersionExpiration": {
        "NoncurrentDays": 15
      },
      "AbortIncompleteMultipartUpload": {
        "DaysAfterInitiation": 0
      },
      "NoncurrentVersionDeletions": {
        "NoncurrentDays": 15
      }
    }
  ]
}

应用方式如下:

aws s3api put-bucket-lifecycle-configuration  \
--bucket 存储桶名称  \
--lifecycle-configuration file://生命周期.json

至于因为我每天只能迭代一次,速度相当慢,嗯,你不能在S3端加速它,所以你可以考虑创建一个基本上删除你不需要的对象的Lambda函数。

你可以根据需要随时运行该函数。

英文:

First things first, you can't have versioning enabled for some objects and exclude other. Either you have it, or you don't.

> When you enable versioning in a bucket, all new objects are versioned and given a unique version ID. Objects that already existed in the bucket at the time versioning was enabled will thereafter always be versioned

Also, keep in mind that:

> Objects that are stored in your bucket before you set the versioning state have a version ID of null. When you enable versioning, existing objects in your bucket do not change.

Having said that, you might actually need more than one rule, but I'm not sure, as your use case is not clear.

Anyhow, when working with more than one rule per bucket, remember that:

> When you have multiple rules in an S3 Lifecycle configuration, an object can become eligible for multiple S3 Lifecycle actions. In such cases, Amazon S3 follows these general rules:

  • Permanent deletion takes precedence over transition.

  • Transition takes precedence over creation of delete markers.

So, summing it up, you might use something like this:

{
  "Rules": [
    {
      "ID": "Delete-Objects-After-15-Days",
      "Filter": {
        "And": {
          "Prefix": "",
          "Tags": [],
          "Not": {
            "Prefix": "PATH/TO/YOUR/OBJECTS/WITH/PREFIX/health-"
          }
        }
      },
      "Status": "Enabled",
      "Transitions": [],
      "NoncurrentVersionTransitions": [],
      "NoncurrentVersionExpiration": {
        "NoncurrentDays": 15
      },
      "AbortIncompleteMultipartUpload": {
        "DaysAfterInitiation": 0
      },
      "NoncurrentVersionDeletions": {
        "NoncurrentDays": 15
      }
    }
  ]
}

Apply it with:

aws s3api put-bucket-lifecycle-configuration  \
--bucket bucketname  \
--lifecycle-configuration file://lifecycle.json

As for since I can iterate only once a day, it's quite slow, well, you can't speed it up on the S3 end, so you might consider creating a Lambda function that basically deletes the objects you don't need.

You can run the function as often as you want / need.

答案2

得分: 1

Thanks @Budaker 对于快速响应,回答了我一些问题,并提供了有用的信息! AWS S3 生命周期规则

> 话虽如此,你可能确实需要多于一个规则,但我不确定,因为你的用例不太清楚。

让我重新表述一下(并根据事实更新它,即我无法避免为带有前缀对象 /client/app/health- 进行版本控制)。

一般情况:

  • 保留当前版本永远不删除。
  • 保留所有非当前版本 15 天,之后删除。
  • 保留所有删除标记 15 天,之后删除。

特殊情况:对于任何以 /client/app/health- 为前缀的文件:

  • 保留当前版本 1 天,之后删除。
  • 保留所有非当前版本 1 天,之后删除。
  • 保留所有删除标记 1 天,之后删除。

考虑到你的示例和解释,你给了我一个一般情况的示例。但我不确定如何处理特殊情况。不过,你给了我足够的材料,能够自己找到解决方案 AWS S3 生命周期规则

我会在几天内编辑此消息,确保它有效。

编辑:以下是规则。再次感谢 @Budaker!

{
    "Rules": [
        {
            "Expiration": {
                "ExpiredObjectDeleteMarker": true
            },
            "ID": "Delete-All-NonCurrent-Version-After-15-Days",
            "Filter": {
                "Prefix": ""
            },
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 15
            },
            "AbortIncompleteMultipartUpload": {
                "DaysAfterInitiation": 1
            }
        },
        {
            "Expiration": {
                "Days": 1
            },
            "ID": "Delete-client1-Health-Object-And-Version-After-1-Day",
            "Filter": {
                "Prefix": "PATH/TO/FILE/health-"
            },
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 1
            },
            "AbortIncompleteMultipartUpload": {
                "DaysAfterInitiation": 1
            }
        }
    ]
}
英文:

Thanks @Budaker for the quick response, which aswer a some questions I had, and gives useful informations! AWS S3 生命周期规则

> Having said that, you might actually need more than one rule, but I'm not sure, > as your use case is not clear.

Let me reformulate (and updating it accordingly to the fact I can't avoid versionning for the prefixed object /client/app/health- ).

General Case :

  • Keep the current version forever
  • Keep all non current version for 15 days. Afterward, delete it.
  • Keep all delete marker for 15 days. Afterward, delete it

Exception Case : For any files prefixed by /client/app/health-

  • Keep the current version for 1 day. Afterward, delete it.
  • Keep all non current version for 1 days. Afterward, delete it.
  • Keep all delete marker for 1 days. Afterward, delete it

Considering your example and explaination, you gave me a sample for the general case. I don't see how I could cover the exception case tho. But you gave me enough material to be able to find a solution on my own AWS S3 生命周期规则

I will edit this message with the full solution in a few days - After testing it to be sure it works.

Edit : Here is the rules. Thanks again @Budaker!

{
    "Rules": [
        {
            "Expiration": {
                "ExpiredObjectDeleteMarker": true
            },
            "ID": "Delete-All-NonCurrent-Version-After-15-Days",
            "Filter": {
                "Prefix": ""
            },
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 15
            },
            "AbortIncompleteMultipartUpload": {
                "DaysAfterInitiation": 1
            }
        },
        {
            "Expiration": {
                "Days": 1
            },
            "ID": "Delete-client1-Health-Object-And-Version-After-1-Day",
            "Filter": {
                "Prefix": "PATH/TO/FILE/health-"
            },
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 1
            },
            "AbortIncompleteMultipartUpload": {
                "DaysAfterInitiation": 1
            }
        }
    ]
}

huangapple
  • 本文由 发表于 2023年5月26日 15:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定