Skip to content

Instantly share code, notes, and snippets.

@greencamp20552
Created July 12, 2023 04:43
Show Gist options
  • Select an option

  • Save greencamp20552/5d2e42d5d3521000d0db4f8c51422225 to your computer and use it in GitHub Desktop.

Select an option

Save greencamp20552/5d2e42d5d3521000d0db4f8c51422225 to your computer and use it in GitHub Desktop.
aws-architect-professional
import boto3
from botocore.exceptions import ClientError
bucket_name = "test-bucket"
folder_paths = ["test_folder","test_folder1", "test_folder2"]
expiration = 1
def add_lifecycle_rule_for_s3(bucket_name,folder_paths):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.get_bucket_lifecycle_configuration(Bucket=bucket_name,)
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'
}
]
}
)
except ClientError as e:
raise Exception( "boto3 client error in get_bucket_lifecycle_of_s3 function: " + e.__str__())
except Exception as e:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment