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 })
מחיקת כל ה Buckets:
s3 = Aws::S3::Resource.new
data = {}
s3.buckets.each do |bucket|
bucket.delete!
end
העברת כל הקבצים מכל ה Buckets ל Bucket יחיד (איחוד באקטים):
def bucket_exists?(buckedef bucket_exists?(bucket)
s3 = Aws::S3::Resource.new
s3.bucket(bucket).exists?
end
# the bucket name to which everything else should be copied
target_bucket_name = ARGV.shift
raise 'Usage: $0 <target_bucket_name>' unless target_bucket_name
s3 = Aws::S3::Client.new
resp = s3.list_buckets
resp.buckets.each do |bucket|
next if bucket.name == target_bucket_name
objects = s3.list_objects(bucket: bucket.name)
objects.contents.each do |item|
src = Addressable::URI.escape('/' + File.join(bucket.name, item.key))
dest = File.join(item.key)
puts "Copying file from #{target_bucket_name} file #{src} TO #{dest}"
s3.copy_object(
bucket: target_bucket_name,
copy_source: src,
key: dest
)
end
end