Last active
September 26, 2020 21:21
-
-
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.
This file contains hidden or 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 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Borrowed inspiration from here: https://gist.github.com/jonathanwcrane/c26ea5c5daf0a156342b