遍历S3存储桶列表和存储桶生命周期配置

时间:2020-04-22 04:16:08

标签: amazon-web-services amazon-s3 command-line-interface

在这里开始编写脚本,所以请对我轻松一点。

我正在寻找一个脚本来动态调用AWS API put-bucket-lifecycle-configuration并自动解析所有200多个存储桶的列表,因此不需要在命令中手动更改存储桶名称。这是我到目前为止的内容:

列出并解析组织中的所有帐户

aws organizations list-accounts

列出并解析所有存储桶

aws s3api list-buckets --query "Buckets[].Name"

用于放置存储桶生命周期配置的命令

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

Lifecycle.json

{
"Rules": [
    {
        "Expiration": {
            "Days": 3650
        },
        "ID": "ExampleRule",
        "Filter": {
            "Prefix": ""
        },
        "Status": "Enabled",
        "Transitions": [
            {
                "Days": 30,
                "StorageClass": "STANDARD_IA"
            },
            {
                "Days": 90,
                "StorageClass": "GLACIER"
            },
            {
                "Days": 180,
                "StorageClass": "DEEP_ARCHIVE"
            }
        ],
        "NoncurrentVersionTransitions": [
            {
                "NoncurrentDays": 30,
                "StorageClass": "STANDARD_IA"
            },
            {
                "NoncurrentDays": 90,
                "StorageClass": "GLACIER"
            },
            {
                "NoncurrentDays": 180,
                "StorageClass": "DEEP_ARCHIVE"
            }
        ],
        "NoncurrentVersionExpiration": {
            "NoncurrentDays": 3650
        },
        "AbortIncompleteMultipartUpload": {
            "DaysAfterInitiation": 7
        }
    }
]

}

1 个答案:

答案 0 :(得分:0)

您可以在每个存储桶名称和put-bucket-lifecycle-configuration上循环。例如:

for bucket in $(aws s3api list-buckets --query "Buckets[].Name" --output text); do

    aws s3api put-bucket-lifecycle-configuration --bucket ${bucket} --lifecycle-configuration file://lifecycle.json

done