Skip to content

Instantly share code, notes, and snippets.

@charsyam
Created January 2, 2020 14:19
Show Gist options
  • Save charsyam/0d5c4d22b117723289040aa8d7d9a98e to your computer and use it in GitHub Desktop.
Save charsyam/0d5c4d22b117723289040aa8d7d9a98e to your computer and use it in GitHub Desktop.
retrive all versions of bucket
import sys
import boto3
from datetime import datetime, timezone
Bucket = 'BUCKET_NAME'
client = boto3.client('s3')
#prefix = sys.argv[1] + "/"
Prefix = ""
now = datetime.now(timezone.utc)
total_size = 0
daily_size = {7: 0, 15: 0, 30: 0}
MaxKeys = 1000
KeyMarker = None
IsTruncated = True
size_map = {7: 0, 15: 0, 30: 0, 3650: 0}
while IsTruncated == True:
if not KeyMarker:
version_list = client.list_object_versions(
Bucket=Bucket,
MaxKeys=MaxKeys,
Prefix=Prefix)
else:
version_list = client.list_object_versions(
Bucket=Bucket,
MaxKeys=MaxKeys,
Prefix=Prefix,
KeyMarker=KeyMarker)
try:
objects = []
versions = version_list['Versions']
for v in versions:
isLatest = v['IsLatest']
size = v['Size']
total_size += size
if not isLatest:
mdate = v['LastModified']
dtime = now - mdate
day, _ = divmod(dtime.total_seconds(), 86400)
day = int(day)
for key in size_map.keys():
if day < key:
size_map[key] += size
IsTruncated = version_list['IsTruncated']
KeyMarker = version_list['NextKeyMarker']
except:
IsTruncated = False
print("total size: ", total_size/1024/1024/1024/1024)
print("cost: ", total_size/1024/1024/1024/1024*1000*0.023)
for key in size_map.keys():
print(key, " days")
v = size_map[key]
print("size: ", v/1024/1024/1024/1024)
print("cost: ", v/1024/1024/1024/1024*1000*0.023)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment