AWS S3: how do I see how much disk space is using

前端 未结 18 1350
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 10:33

I have AWS account. I\'m using S3 to store backups from different servers. The question is there any information in the AWS console about how much disk space is in use in my

18条回答
  •  失恋的感觉
    2020-12-12 10:44

    On linux box that have python (with pip installer), grep and awk, install AWS CLI (command line tools for EC2, S3 and many other services)

    sudo pip install awscli
    

    then create a .awssecret file in your home folder with content as below (adjust key, secret and region as needed):

    [default]
    aws_access_key_id=
    aws_secret_access_key=
    region=
    

    Make this file read-write to your user only:

    sudo chmod 600 .awssecret
    

    and export it to your environment

     export AWS_CONFIG_FILE=/home//.awssecret
    

    then run in the terminal (this is a single line command, separated by \ for easy reading here):

    aws s3 ls s3:///foo/bar | \
    grep -v -E "(Bucket: |Prefix: |LastWriteTime|^$|--)" | \
    awk 'BEGIN {total=0}{total+=$3}END{print total/1024/1024" MB"}'
    
    • the aws part lists the bucket (or optionally a 'sub-folder')
    • the grep part removes (using -v) the lines that match the Regular Expression (using -E). ^$ is for blank line, -- is for the separator lines in the output of aws s3 ls
    • the last awk simply add to total the 3rd colum of the resulting output (the size in KB) then display it at the end

    NOTE this command works for the current bucket or 'folder', not recursively

提交回复
热议问题