Created
August 9, 2020 19:41
-
-
Save prasvats/1394f640f27504a42c07c33307e30030 to your computer and use it in GitHub Desktop.
S3LifeCycle.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
from botocore.exceptions import ClientError | |
session = boto3.Session(profile_name='tsample', region_name='ap-southeast-2') | |
client = session.client('s3') | |
try: | |
all_buckets = client.list_buckets() | |
policy_status = client.put_bucket_lifecycle_configuration( | |
Bucket='sample-compute-optimizer', | |
LifecycleConfiguration={ | |
'Rules': [ | |
{ | |
'Expiration': { | |
'Days': 30 | |
}, | |
'ID': 'Expire After 30 Days', | |
'Filter': { | |
'Prefix': '', | |
}, | |
'Status': 'Enabled', | |
'NoncurrentVersionExpiration': { | |
'NoncurrentDays': 7 | |
}, | |
'AbortIncompleteMultipartUpload': { | |
'DaysAfterInitiation': 7 | |
} | |
}, | |
{ | |
'ID': 'Transit After 90 Days for Current and 60 Days for Noncurrent', | |
'Filter': { | |
'Prefix': '', | |
}, | |
'Status': 'Enabled', | |
'Transitions': [ | |
{ | |
'Days': 90, | |
'StorageClass': 'GLACIER' | |
}, | |
], | |
'NoncurrentVersionTransitions': [ | |
{ | |
'NoncurrentDays': 60, | |
'StorageClass': 'GLACIER' | |
}, | |
] | |
} | |
]}) | |
except ClientError as e: | |
print("Unable to apply bucket policy. \nReason:{0}".format(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment