Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Created June 27, 2018 09:56
Show Gist options
  • Select an option

  • Save jacoyutorius/bfbcb575adde8343c4856c32cf57b113 to your computer and use it in GitHub Desktop.

Select an option

Save jacoyutorius/bfbcb575adde8343c4856c32cf57b113 to your computer and use it in GitHub Desktop.
S3のバケットサイズを取得するRubyスクリプト(AWS CLI使用)
class S3Info
require "json"
require "pstore"
attr_reader :info
def initialize
@info = JSON.parse(`aws s3api list-buckets`)
end
def buckets
@_buckets ||= info["Buckets"].map{|b| Bucket.new(name: b["Name"], creation_date: b["CreationDate"]) }
end
def total_size
@_total_size ||= buckets.inject(0){|sum, bucket| sum += bucket.total_size }
end
def save_info
store = Store.new
store.save(s3_info: self)
end
def load_info
store = Store.new
data = store.load
@_buckets = data.buckets
@_total_size = data.total_size
end
class Store
attr_reader :pstore
S3InfoStruct = Struct.new(:buckets, :total_size)
def initialize path: nil
path ||= "#{Dir.pwd}/store.db"
@pstore = PStore.new(path)
end
def save s3_info: nil
pstore.transaction do
pstore[:buckets] = s3_info.buckets
pstore[:total_size] = s3_info.total_size
end
end
def load
struct = S3InfoStruct.new()
pstore.transaction do
struct.buckets = pstore[:buckets]
struct.total_size = pstore[:total_size]
end
struct
end
end
class Bucket
attr_reader :name, :creation_date
def initialize name: nil, creation_date: nil
raise if name.nil? || creation_date.nil?
@name = name
@creation_date = creation_date
end
def info
@_info ||= `aws s3 ls s3://#{name} --recursive --summarize`.split("\n").map{|s| s.gsub(" ", "") }
end
def total_size
info.last.match(/TotalSize:(?<total_size>\d*)/)[:total_size].to_f
end
end
end
if $0 == __FILE__
require "pp"
info = S3Info.new
# info.save_info
info.load_info
info.buckets.each{|b|
pp b.total_size
}
pp "Total Size: #{info.total_size}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment