Class: Awful::S3

Inherits:
Cli show all
Defined in:
lib/awful/s3.rb

Instance Method Summary collapse

Methods inherited from Cli

#initialize

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#buckets(name = /./) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/awful/s3.rb', line 35

def buckets(name = /./)
  s3.list_buckets.buckets.select do |bucket|
    bucket.name.match(/#{name}/)
  end.output do |list|
    if options[:long]
      print_table list.map { |b| [ b.name, b.creation_date ] }
    else
      puts list.map(&:name)
    end
  end
end

#cat(path) ⇒ Object



99
100
101
102
103
104
# File 'lib/awful/s3.rb', line 99

def cat(path)
  bucket, key = path.split('/', 2)
  s3.get_object(bucket: bucket, key: key) do |chunk|
    $stdout.write(chunk)
  end
end

#clean(name) ⇒ Object



172
173
174
175
176
# File 'lib/awful/s3.rb', line 172

def clean(name)
  if yes? "Really delete ALL objects in bucket #{name}?", :yellow
    clean_objects(name)
  end
end

#empty?(bucket_name) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/awful/s3.rb', line 64

def empty?(bucket_name)
  s3.list_objects(bucket: bucket_name, max_keys: 1).contents.empty?.output(&method(:puts))
end

#exists?(bucket_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/awful/s3.rb', line 55

def exists?(bucket_name)
  begin
    s3.head_bucket(bucket: bucket_name) && true
  rescue Aws::S3::Errors::NotFound
    false
  end.output(&method(:puts))
end

#get(bucket, key, filename = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/awful/s3.rb', line 108

def get(bucket, key, filename = nil)
  if filename
    s3.get_object(bucket: bucket, key: key, response_target: filename)
  else
    s3.get_object(bucket: bucket, key: key).output do |response|
      puts response.body.read
    end
  end
end

#ls(name = '.') ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/awful/s3.rb', line 24

def ls(name = '.')
  if name.include?('/')
    bucket, prefix = name.split('/', 2)
    invoke 'objects', [bucket, prefix], options
  else
    invoke 'buckets', [name], options
  end
end

#objects(bucket, prefix = nil) ⇒ Object



48
49
50
51
52
# File 'lib/awful/s3.rb', line 48

def objects(bucket, prefix = nil)
  s3_resource.bucket(bucket).objects(prefix: prefix).map do |object|
    object.key
  end.output { |list| puts list }
end

#put(bucket, key, filename = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/awful/s3.rb', line 129

def put(bucket, key, filename = nil)
  body = options.fetch('string', file_or_stdin(filename))
  s3.put_object(
    bucket: bucket,
    key: key,
    body: body,
    server_side_encryption: options[:kms] ? 'aws:kms' : nil,
    ssekms_key_id: options[:kms],
  )
end

#rbObject

rb is an alias for remove_bucket



148
# File 'lib/awful/s3.rb', line 148

map :rb => :remove_bucket

#remove_bucket(name) ⇒ Object



141
142
143
144
145
# File 'lib/awful/s3.rb', line 141

def remove_bucket(name)
  if yes? "Really delete bucket #{name}?", :yellow
    s3.delete_bucket(bucket: name)
  end
end

#tagged(name = '.') ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/awful/s3.rb', line 72

def tagged(name = '.')
  conditions = options[:tags].map do |tag|
    key, value = tag.split('=')
    ->(set) { (set[0] == key) && (set[1] == value) }
  end
  if options[:stack]
    conditions << ->(set) { (set[0] == 'aws:cloudformation:stack-name') && (set[1] == options[:stack]) }
  end
  if options[:resource]
    conditions << ->(set) { (set[0] == 'aws:cloudformation:logical-id') && (set[1] == options[:resource]) }
  end

  ## get all buckets and check for a match with conditions
  s3.list_buckets.buckets.select do |b|
    b.name.match(/^#{name}/i)
  end.map do |bucket|
    tags = get_tags(bucket.name) or next
    tags.any? do |set|
      conditions.any? { |c| c.call(set) }
    end && bucket
  end.select{|b| b}.output do |buckets|
    puts buckets.map(&:name)
  end
end

#upload(file, s3path) ⇒ Object



120
121
122
123
# File 'lib/awful/s3.rb', line 120

def upload(file, s3path)
  bucket, key = s3path.split('/', 2)
  s3_resource.bucket(bucket).object(key).upload_file(file)
end