Class: Awry::S3

Inherits:
Cli
  • Object
show all
Defined in:
lib/awry/s3.rb

Constant Summary collapse

COLORS =
{
  active: :green,
  available: :green,
  deleted: :red,
  expired: :red,
  failed: :red,
  rejected: :red,
}

Instance Method Summary collapse

Instance Method Details

#acl(bucket) ⇒ Object



78
79
80
81
82
83
84
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# 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

#delete(bucket) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



50
51
52
53
54
# 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) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/awry/s3.rb', line 29

def ls(prefix = nil)
  if prefix&.include?('/')
    p client
  else
    buckets(prefix)
  end
end

#policy(bucket) ⇒ Object



71
72
73
74
75
# 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