Как удалить объекты в корзине S3 на основе заданных критериев с помощью политики корзины?
- Total object counts in the bucket should be more than 5 everytime.
- Object should have a common prefix.
- Object age should be more than X(1,2,3,4...n) days.
Конструкция ковша -
myprefixtest1234bucket //bucket
|- prefixtestobject1 //object
|- prefixtestobject2
|- prefixtestobject3
|- prefixtestobject4
|- prefixtestobject5
|- prefixtestobject6
|- prefixtestobject7
|- testobject8
|- testobject9
I am trying to delete the object in a bucket which has prefix "prefixtext" and is older than X days and this bucket should keep Y numbers of object all the time even if it is older than X days. Means keeping Y number of object should be given precedence over age (older than X days).
Я пробовал политику ниже, но не работал и не знал, как добавить логику возраста и подсчета объектов.
{
"Id": "Policy123456",
"Version":"2012-10-17",
"Statement":[
{
"Sid":"TestBucketObjectDeletion",
"Effect":"Allow",
"Principal": {"AWS": ["arn:aws:iam::123456789:root"]},
"Action":["s3:DeleteObject"],
"Resource":["arn:aws:s3:::myprefixtest1234/*"],
"Condition":{"StringEquals":{"myprefixtest1234"}}
}
]
}





Currently, there is no way to specify to keep number(1,2,3....n) of objects in the bucket and delete rest of them based on common prefix but we can set the expiration of the bucket objects based on age using bucket Life Cycle.
{
"Rules": [
{
"Filter": {
"Prefix": "documents/"
},
"Status": "Enabled",
"Transitions": [
{
"Days": 365,
"StorageClass": "GLACIER"
}
],
"Expiration": {
"Days": 3650
},
"ID": "ExampleRule"
}
]
}
И поместите его с помощью s3api вот так -
aws s3api put-bucket-lifecycle-configuration \
--bucket bucketname \
--lifecycle-configuration file://lifecycle.json