Skip to content

Instantly share code, notes, and snippets.

@natchiketa
Last active June 5, 2017 11:24
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 natchiketa/588bdb9caf193da757515e622b0fbc22 to your computer and use it in GitHub Desktop.
Save natchiketa/588bdb9caf193da757515e622b0fbc22 to your computer and use it in GitHub Desktop.
Get the size of your S3 Buckets with the AWS cli
# SETUP
# assumes macOS and Homebrew are installed
# Get Python 3
brew install python3
# Get pip (pip3 for Python 3)
curl -O https://bootstrap.pypa.io/get-pip.py
# Use pip3 to get the awscli package
pip3 install --user --upgrade awscli
# I had to add the following line to my dotfiles (i.e. my .bash_profile) to add it to my $PATH:
PATH="$HOME/Library/Python/3.6/bin:$PATH"
# If it's been a fair amount of time since June of 2017, maybe look in ~/Library/Python and make sure it's still v3.6
# Check that it installed properly
aws --version
# If it's good, you should see something like:
# aws-cli/1.11.97 Python/3.6.1 Darwin/16.6.0 botocore/1.5.60
# AWS CLI CONFIGURATION
# Configure an account with enough access to do stuff from
# the CLI (in AWS IAM), and get an access key and secret.
# Also, for your convenience, here are the abbreviations
# for some AWS regions:
# US East (N. Virginia) us-east-1
# US West (Oregon) us-west-2
# EU (Ireland) eu-west-1
# Asia Pacific (Tokyo) ap-northeast-1
aws configure
# You should get a wizard that'll go something like this:
# AWS Access Key ID [None]: PUT_YOUR_ACCESS_KEY_HERE
# AWS Secret Access Key [None]: PUT_YOUR_SECRET_HERE
# Default region name [None]: REFER_TO_THE_TABLE
# Default output format [None]: json
# GETTING BUCKET SIZE (IN BYTES)
aws s3api list-objects --bucket YOUR_BUCKET_NAME --query "[sum(Contents[].Size), length(Contents[])]"
# If you didn't choose JSON as your default output format
# (see file above), you'll also need to add `--output json`
# to the line above.
# You should see (after a bit of calculation, maybe a lot
# depending on how flat your directory structure is on your
# bucket:
# [
# 447530101701,
# 32208
# ]
# Where the first number is the total size of the objects in
# your bucket, and the second number is the total number of
# objects.
# CONVERT BYTES TO GIGABYTES (WITH NODE.JS)
node -e "console.log(447530101701 * 1e-9)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment