Class: Awful::S3

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

Instance Method Summary collapse

Methods inherited from Cli

#initialize, #ll, #version

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#buckets(name = /./) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/awful/s3.rb', line 49

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



113
114
115
116
117
118
# File 'lib/awful/s3.rb', line 113

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



199
200
201
202
203
# File 'lib/awful/s3.rb', line 199

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

#copy(bucket, key) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/awful/s3.rb', line 157

def copy(bucket, key)
  s3.copy_object(
    bucket: bucket,
    key: key,
    server_side_encryption: options[:kms] ? 'aws:kms' : nil,
    ssekms_key_id: options[:kms],
    copy_source: options[:source],
  )
end

#delete(bucket, *objects) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/awful/s3.rb', line 206

def delete(bucket, *objects)
  s3.delete_objects(
    bucket: bucket,
    delete: {
      objects: objects.map{ |k| {key: k} }
    }
  )
end

#empty?(bucket_name) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/awful/s3.rb', line 78

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)


69
70
71
72
73
74
75
# File 'lib/awful/s3.rb', line 69

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



122
123
124
125
126
127
128
129
130
# File 'lib/awful/s3.rb', line 122

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



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

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



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

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

#orphansObject



217
218
219
220
221
222
223
224
225
# File 'lib/awful/s3.rb', line 217

def orphans
  s3.list_buckets.buckets.each do |bucket|
    if (stack = get_stack_tag(bucket.name))
      next if stack_exists?(stack)
      puts bucket.name
      s3.delete_bucket(bucket: bucket.name) if options[:delete]
    end
  end
end

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



143
144
145
146
147
148
149
150
151
152
# File 'lib/awful/s3.rb', line 143

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



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

map :rb => :remove_bucket

#remove_bucket(name) ⇒ Object



168
169
170
171
172
# File 'lib/awful/s3.rb', line 168

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

#tagged(name = '.') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/awful/s3.rb', line 86

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



134
135
136
137
# File 'lib/awful/s3.rb', line 134

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