Skip to content

Instantly share code, notes, and snippets.

@nijine
Last active September 26, 2020 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nijine/581ee63dde89cc56c7a32d3bdbcecbe5 to your computer and use it in GitHub Desktop.
Save nijine/581ee63dde89cc56c7a32d3bdbcecbe5 to your computer and use it in GitHub Desktop.
Quick and dirty way to output the sum of all multipart uploads in an s3 bucket, optionally limiting the output to objects that match a given prefix.
import boto3
from sys import argv, exit
def get_bucket_mpus_size(bucket_name, object_prefix=''):
aws_session = boto3.session.Session() # Uses either EC2/ECS metadata, ~/.aws/credentials file, or env vars for auth.
s3 = aws_session.resource('s3')
bytes_per_gb = 1024 * 1024 * 1024
bucket = s3.Bucket(bucket_name)
bucket_mpu_gb = 0
for mpu in bucket.multipart_uploads.all():
#print(mpu.initiated)
if object_prefix in mpu.object_key:
mpu_size = 0
for part in mpu.parts.all():
mpu_size += part.size
mpu_gb = mpu_size / bytes_per_gb
print(bucket.name+","+str(mpu.initiated)+','+mpu.object_key+','+str(mpu_gb))
bucket_mpu_gb += mpu_gb
print("bucket",bucket.name,bucket_mpu_gb,"GB")
def main():
if len(argv) == 2:
get_bucket_mpus_size(argv[1])
elif len(argv) == 3:
get_bucket_mpus_size(argv[1], argv[2])
else:
print("""
Usage: python3 bucket_mpu.py <bucket_name> [object_prefix]
Example: python3 bucket_mpu.py reonomy-viserion-prd Meridian
This will output stats for each mulipart upload part, as well as the total of every result returned.
""")
exit(1)
if __name__ in '__main__':
main()
@nijine
Copy link
Author

nijine commented Sep 26, 2020

@jonathanwcrane
Copy link

Nice work @nijine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment