Last active
August 26, 2020 14:17
-
-
Save addomafi/5c85757dd2dcb9ea1c7b04f2edf421e7 to your computer and use it in GitHub Desktop.
Describe all S3 buckets and the lifecycles policies
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 sys | |
import boto3, json, copy | |
from datetime import datetime, timedelta | |
import dateutil.parser | |
from time import sleep | |
session = boto3.Session(profile_name='bkp-default', region_name='us-east-1') | |
s3 = session.client('s3') | |
# less /Users/admartins/LocalDocuments/notes/s3_bucket_lifecycle_2020-07-02.log | jq -r '.[] | [.BucketName, .VersioningEnabled, .Prefix, .TransitionDays, .TransitionValidUntil, .TransitionStorage, .ExpirationDays, .ExpirationValidUntil] | @tsv' | pbcopy | |
AWS_NAMESPACE = 'AWS/S3' | |
def getValue(key, map, default=""): | |
if key in map: | |
return map[key] | |
return default | |
def get_bucket_lifecycle(): | |
response = s3.list_buckets() | |
bucketData = [] | |
# For each metric get data | |
for bucket in response['Buckets']: | |
bucketName = bucket['Name'] | |
print("Getting metrics for bucket: {}".format(bucketName)) | |
untilFrom = bucket['CreationDate'] | |
sleep(0.5) | |
try: | |
versioning = s3.get_bucket_versioning( | |
Bucket=bucketName | |
) | |
except: | |
versioning = dict() | |
data = { | |
"BucketName": bucketName, | |
"VersioningEnabled": getValue('Status', versioning, default="Disabled") | |
} | |
try: | |
sleep(0.5) | |
lifecyleRules = s3.get_bucket_lifecycle( | |
Bucket=bucketName | |
) | |
for rule in lifecyleRules['Rules']: | |
if rule['Status'] == 'Enabled': | |
newData = copy.deepcopy(data) | |
bucketData.append(newData) | |
newData['Prefix'] = getValue('Prefix', rule, default="*") | |
if 'Expiration' in rule: | |
newData['ExpirationDays'] = getValue('Days', rule['Expiration']) | |
newData['ExpirationValidUntil'] = getValue('Date', rule['Expiration']) | |
if 'Transition' in rule: | |
newData['TransitionDays'] = getValue('Days', rule['Transition']) | |
newData['TransitionValidUntil'] = getValue('Date', rule['Transition']) | |
newData['TransitionStorage'] = getValue('StorageClass', rule['Transition']) | |
except: | |
bucketData.append(data) | |
return bucketData | |
if __name__ == '__main__': | |
bucket_sizes = get_bucket_lifecycle() | |
text_file = open("/Users/admartins/LocalDocuments/notes/s3_bucket_lifecycle_{}.log".format(sys.argv[1]), "w") | |
text_file.write(json.dumps(bucket_sizes, ensure_ascii=False)) | |
text_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment