/ ruby

S3 Beginner Scripts

AWS S3 has a very fun and easy API to work with, but the management console is somewhat lacking. I couldn't find a fast way to list my buckets and their sizes, or to delete all buckets. So instead I scripted it in ruby.

Listing All Bucket Sizes

Getting the total size of all your buckets require very little code in ruby. The only hard part here was to display the size in human readable format.

require 'pp'

UNITS = %w(B KiB MiB GiB TiB).freeze

def as_size(number)
  if number.to_i < 1024
    exponent = 0

  else
    max_exp  = UNITS.size - 1

    exponent = (Math.log(number) / Math.log(1024)).to_i # convert to base
    exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit

    number  /= 1024**exponent
  end

  "#{number} #{UNITS[exponent]}"
end

s3 = Aws::S3::Resource.new
data = {}

s3.buckets.each do |bucket|
  s = 0
  bucket.objects.each {|o| s += o.size }
  data[bucket.name] = as_size(s)
end

pp(data.sort_by {|_key, value| value })

Delete All Buckets

Another simple task I couldn't find in the management console. This time it's even shorter (as there's no need to display size):

s3 = Aws::S3::Resource.new
data = {}

s3.buckets.each do |bucket|
    bucket.delete!
end