我想使用 lambda 和 boto3 向 S3 存盤桶添加多個生命周期規則。但是,使用 boto3,它似乎只允許您添加 1 個生命周期規則,這也會覆寫已經存在的規則。
例如,執行以下操作只會覆寫我擁有的任何預先存在的規則以及新撰寫的規則,并且只保留串列中的最后一個:
bucket_name = "test-bucket"
folder_paths = ["test_folder","test_folder1", "test_folder2"]
expiration = 1
for folder_path in folder_paths:
client = boto3.client('s3')
response = client.put_bucket_lifecycle_configuration(
Bucket=bucket_name,
LifecycleConfiguration={
'Rules': [
{
'Expiration': {
'Days': expiration
},
'ID': folder_path,
'Filter': {
'Prefix': folder_path
},
'Status': 'Enabled'
}
]
}
)
當然,使用 AWS 控制臺可以在一個存盤桶上添加多個單獨的 S3 生命周期配置。
與 類似put_bucket_lifecycle_configuration,我也嘗試過put_bucket_lifecycle,這給了我相同的結果。
有什么方法可以使用 boto3 在存盤桶上添加多個 S3 生命周期配置?我錯過了一些明顯的東西嗎?
任何幫助表示贊賞,并提前感謝!
uj5u.com熱心網友回復:
當然,使用 AWS 控制臺可以在一個存盤桶上添加多個單獨的 S3 生命周期配置。
每個存盤桶都有 1 個生命周期配置,最多可以有 1000 條規則。您的控制臺可能會顯示類似的內容:

這些不是不同的生命周期配置,它們是同一生命周期配置的不同規則部分。
在輸入中,put_bucket_lifecycle_configuration我們可以看到我們可以傳遞一個規則串列,其中可以包含 1 個或更多(最多 1000 個)規則。
for folder_path in folder_paths:
client = boto3.client('s3')
response = client.put_bucket_lifecycle_configuration(
Bucket=bucket_name,
LifecycleConfiguration={
'Rules': [
{
'Expiration': {
'Days': expiration
},
'ID': id_rule_1,
'Filter': {
'Prefix': folder_path
},
'Status': 'Enabled'
},
{
'Expiration': {
'Days': expiration2
},
'ID': id_rule_2,
'Filter': {
'Prefix': folder_path2
},
'Status': 'Enabled'
},
...
]
}
)
正如檔案所說,put_bucket_lifecycle_configuration“為存盤桶創建新的生命周期配置或替換現有的生命周期配置。 ”如果要更新生命周期配置,則必須使用get_bucket_lifecycle_configuration檢索現有規則,修改它們然后使用put_bucket_lifecycle_configuration覆寫現有配置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/416572.html
標籤:
