class Awry::S3

Constants

COLORS

Public Instance Methods

acl(bucket) click to toggle source
# File lib/awry/s3.rb, line 78
def acl(bucket)
  client.get_bucket_acl(bucket: bucket).grants.map do |g|
    [ g.grantee.display_name, g.grantee.id, g.grantee.type, g.permission ]
  end.tap do |list|
    print_table list
  end
end
buckets(prefix = nil) click to toggle source
# File lib/awry/s3.rb, line 38
def buckets(prefix = nil)
  client.list_buckets.buckets.tap do |buckets|
    buckets.select! { |b| b.name.start_with?(prefix) } if prefix
  end.map do |b|
    region = client.get_bucket_location(bucket: b.name).location_constraint
    [ b.name, region, b.creation_date ]
  end.tap do |list|
    print_table list.sort
  end
end
client() click to toggle source
# File lib/awry/s3.rb, line 15
def client
  @_client ||= Aws::S3::Client.new
end
delete(bucket) click to toggle source
# File lib/awry/s3.rb, line 58
def delete(bucket)
  if options[:empty]
    empty(bucket)
    wait_until_empty(bucket)
  end
  if yes?("Really delete bucket #{bucket}?", :yellow)
    client.delete_bucket(bucket: bucket)
  end
rescue Aws::S3::Errors::BucketNotEmpty => e
  error(e.message)
end
empty(bucket) click to toggle source
# File lib/awry/s3.rb, line 50
def empty(bucket)
  if yes?("Really delete objects and versions from bucket #{bucket}?", :yellow)
    Aws::S3::Resource.new.bucket(bucket).object_versions.batch_delete!
  end
end
ls(prefix = nil) click to toggle source
# File lib/awry/s3.rb, line 29
def ls(prefix = nil)
  if prefix&.include?('/')
    p client
  else
    buckets(prefix)
  end
end
policy(bucket) click to toggle source
# File lib/awry/s3.rb, line 71
def policy(bucket)
  client.get_bucket_policy(bucket: bucket).policy.tap do |policy|
    puts JSON.pretty_generate(JSON.parse(policy.gets))
  end
end
wait_until_empty(bucket) click to toggle source
# File lib/awry/s3.rb, line 19
def wait_until_empty(bucket)
  while true
    break if client.list_objects_v2(bucket: bucket, max_keys: 1).key_count == 0
    puts 'waiting for objects to delete'
    sleep 3
  end
end