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

问题描述 投票:0回答:1

[这里是开始编写脚本的人,所以请放轻松。

我正在寻找一个脚本来动态调用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
        }
    }
]

}

amazon-web-services amazon-s3 command-line-interface
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.