Last active
May 20, 2020 14:47
-
-
Save kshailen/8d02a221328c48933ded65b2f2703da8 to your computer and use it in GitHub Desktop.
This script will lifecycle rule to clean multipart uploads
This file contains hidden or 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 | |
import sys | |
import json | |
from botocore.exceptions import ClientError | |
def delete_multipart_uploads(bucketName): | |
s3 = boto3.client('s3') | |
try: | |
bucket_lifecycle_response = s3.get_bucket_lifecycle(Bucket=bucketName) | |
rules = bucket_lifecycle_response["Rules"] | |
# print(rules) | |
lifecycle = {'Rules': rules} | |
except ClientError: | |
lifecycle = {'Rules': []} | |
new_rule = { | |
'ID': 'DeleteMultipartUploads_older_than_7_days', | |
'Status': 'Enabled', | |
'Prefix': '', | |
'AbortIncompleteMultipartUpload': { | |
'DaysAfterInitiation': 7 | |
} | |
} | |
if new_rule not in lifecycle["Rules"]: | |
lifecycle['Rules'].append(new_rule) | |
try: | |
response = s3.put_bucket_lifecycle( | |
Bucket=bucketName, LifecycleConfiguration=lifecycle) | |
if response["ResponseMetadata"]["HTTPStatusCode"] == 200: | |
print("Successfully updated Lifecycle Policy of Bucket : " + bucketName) | |
except ClientError: | |
print("Something went worng for bucket : " + bucketName) | |
print("Status code :" + | |
str(response["ResponseMetadata"]["HTTPStatusCode"])) | |
def main(): | |
print("Updating Policy for deleting multipart uploads") | |
delete_multipart_uploads("multipart-test-sk") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment