feat: bucket size is now reported
This commit is contained in:
@@ -8,6 +8,43 @@ License: This program is released under the MIT License
|
||||
# Imports
|
||||
import boto3
|
||||
from openpyxl import load_workbook
|
||||
import datetime
|
||||
|
||||
|
||||
def getBucketSize(bucket: str) -> int:
|
||||
"""
|
||||
Get size of a s3 bucket through cloudwatch metric
|
||||
|
||||
:param bucket:
|
||||
:return: Bucket size in MiB
|
||||
"""
|
||||
s3 = boto3.client('s3')
|
||||
region = s3.get_bucket_location(Bucket=bucket)['LocationConstraint']
|
||||
if region is None:
|
||||
region='us-east-1'
|
||||
cloudwatch_client = boto3.client('cloudwatch', region_name=region)
|
||||
response = cloudwatch_client.get_metric_statistics(Namespace='AWS/S3',
|
||||
MetricName='BucketSizeBytes',
|
||||
Dimensions=[
|
||||
{
|
||||
'Name': 'BucketName',
|
||||
'Value': bucket
|
||||
},
|
||||
{
|
||||
'Name': 'StorageType',
|
||||
'Value': 'StandardStorage'
|
||||
}
|
||||
],
|
||||
StartTime=datetime.datetime.now() - datetime.timedelta(
|
||||
days=7),
|
||||
EndTime=datetime.datetime.now(),
|
||||
Period=86400,
|
||||
Statistics=['Average']
|
||||
)
|
||||
if len(response['Datapoints']) == 0:
|
||||
return 0
|
||||
else:
|
||||
return int(response['Datapoints'][0]['Average']/1024/1024/1024)
|
||||
|
||||
# Main function
|
||||
def main() -> None:
|
||||
@@ -21,9 +58,9 @@ def main() -> None:
|
||||
response = s3_client.list_buckets()
|
||||
|
||||
# Print the names of the buckets
|
||||
ws.append(['Buckets'])
|
||||
ws.append(['Buckets', 'SizeGiB'])
|
||||
for bucket in response['Buckets']:
|
||||
ws.append([bucket["Name"]])
|
||||
ws.append([bucket["Name"], getBucketSize(bucket["Name"])])
|
||||
|
||||
wb.save('aws-inventory.xlsx')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user