-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_versioning_cost.py
More file actions
89 lines (63 loc) · 1.91 KB
/
s3_versioning_cost.py
File metadata and controls
89 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Get the total number of bytes used to store non-current versions
of S3 objects in a bucket.
The result of this tool needs to be multiplied by the S3 pricing
associated with your bucket.
https://aws.amazon.com/s3/pricing/
"""
import sys
import argparse
import boto3
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
'--bucket',
help='S3 bucket name',
required=True
)
parser.add_argument(
'--profile',
help='AWS profile from ~/.aws/credentials',
required=True
)
args = parser.parse_args()
try:
session = boto3.Session(profile_name=args.profile)
except Exception as e:
print('%s' % e)
sys.exit(1)
return args.bucket, session
def get_size_for_previous_versions(session, bucket):
total_size = 0
iter_count = 0
version_count = 0
non_current_versions = 0
s3 = session.client('s3')
paginator = s3.get_paginator('list_object_versions')
response_iterator = paginator.paginate(Bucket=bucket)
for response in response_iterator:
versions = response.get('Versions')
if versions is None:
continue
iter_count += 1
if iter_count % 50 == 0:
stats = {
'analyzed_objects': version_count,
'non_current_objects': non_current_versions,
'non_current_objects_size_bytes': total_size
}
print(stats)
for version in versions:
version_count += 1
if version['IsLatest']:
# We just want the cost for the previous versions
continue
total_size += version['Size']
non_current_versions += 1
return total_size
def main():
bucket, session = parse_arguments()
total = get_size_for_previous_versions(session, bucket)
print(total)
if __name__ == '__main__':
main()