Class: AwsRegion::AwsBucket

Inherits:
AwsBase
  • Object
show all
Defined in:
lib/aws_region.rb

Overview

Methods for dealing with S3 buckets

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AwsBase

#log

Constructor Details

#initialize(region, options = {}) ⇒ AwsBucket

Constructs a bucket instance from an existing bucket, or creates a new one with the name

  • :id - id of existing bucket

  • :bucket - Name of bucket to find or create

Parameters:

  • region (String)
    • Value from REGION static hash

  • options (Hash) (defaults to: {})
    • Possible values:



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/aws_region.rb', line 183

def initialize(region, options={})
  @region = region
  if options.has_key?(:id)
    @id = options[:id]
  elsif options.has_key?(:bucket)
    bucket = options[:bucket]
    if @region.find_buckets({bucket: bucket}).length <= 0
      @region.s3.create_bucket({:bucket => bucket,
                                :create_bucket_configuration => {:location_constraint => @region.region}})
      if @region.find_buckets({bucket: bucket}).length <= 0
        raise "Error creating bucket: #{bucket} in region: #{@region.region}"
      end
    end
    @id = bucket
  end
end

Instance Attribute Details

#regionObject

Returns the value of attribute region.



176
177
178
# File 'lib/aws_region.rb', line 176

def region
  @region
end

Instance Method Details

#deleteAwsPageableResponse

Delete this bucket instance

Returns:

  • (AwsPageableResponse)

    ]



202
203
204
# File 'lib/aws_region.rb', line 202

def delete
  @region.s3.delete_bucket({bucket: @id})
end

#delete_all_objectsBoolean

delete all objects in a bucket

Returns:

  • (Boolean)


309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/aws_region.rb', line 309

def delete_all_objects
  begin
    response = @region.s3.list_objects({:bucket => @id})
    response[:contents].each do |obj|
      @region.s3.delete_object(:bucket => @id,
                               :key => obj[:key])
    end
  rescue Exception => e
    return false
  end
  true
end

#delete_object(options = {}) ⇒ Boolean

deletes from s3 an object at :s3_path_to_object

  • :s3_path_to_object

Parameters:

  • options (Hash) (defaults to: {})
    • Can be:

Returns:

  • (Boolean)


295
296
297
298
299
300
301
302
303
304
305
# File 'lib/aws_region.rb', line 295

def delete_object(options={})
  begin
    s3_path_to_object = options[:s3_path_to_object]
    log "s3 delete  #{s3_path_to_object}"
    @region.s3.delete_object(:bucket => @id,
                             :key => s3_path_to_object)
  rescue Exception => e
    return false
  end
  true
end

#find(options = {}) ⇒ Array<Hash>

prefix is something like: hchd-A-A-Items This will return in an array of strings the names of all objects in s3 in the :aws_path under :bucket starting with passed-in prefix example: :aws_path=>‘development’, :prefix=>‘broadhead’

would return array of names of objects in said bucket
matching (in regex terms) development/broadhead.*
  • :aws_path - first part of S3 path to search

  • :prefix - Actually suffix of path to search.

Parameters:

  • options (Hash) (defaults to: {})
    • Can contain:

Returns:

  • (Array<Hash>)
    • 0 or more objects



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/aws_region.rb', line 251

def find(options={})
  aws_path = options[:aws_path]
  prefix = options[:prefix]
  aws_path = '' if aws_path.nil?
  aws_path = aws_path[0..-2] if aws_path[-1..-1] == '/'
  log "s3 searching bucket:#{@id} for #{aws_path}/#{prefix}"
  objects = @region.s3.list_objects(:bucket => @id,
                                    :prefix => "#{aws_path}/#{prefix}")
  f = objects.contents.collect(&:key)
  log "s3 searched  got: #{f.inspect}"
  f
end

#get(options = {}) ⇒ Boolean

writes contents of S3 object to local file example: get( :s3_path_to_object=>development/myfile.txt’,

     :dest_file_path=>'/tmp/foo.txt')
would write to local /tmp/foo.txt a file retrieved from s3
at development/myfile.txt
  • :s3_path_to_object - S3 object path

  • :dest_file_path - local file were file will be written

Parameters:

  • options (Hash) (defaults to: {})
    • Can contain:

Returns:

  • (Boolean)


273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/aws_region.rb', line 273

def get(options={})
  begin
    s3_path_to_object = options[:s3_path_to_object]
    dest_file_path = options[:dest_file_path]
    File.delete dest_file_path if File.exists?(dest_file_path)
    log "s3 get bucket:#{@id} path:#{s3_path_to_object} dest:#{dest_file_path}"
    response = @region.s3.get_object(:bucket => @id,
                                     :key => s3_path_to_object)
    response.body.rewind
    File.open(dest_file_path, 'wb') do |file|
      response.body.each { |chunk| file.write chunk }
    end
  rescue Exception => e
    return false
  end
  true
end

#put(local_file_path, aws_path, options = {}) ⇒ Object

puts a local file to an s3 object in bucket on path example: put_local_file(:bucket=>“bucket”, :local_file_path=>“/tmp/bar/foo.txt”, :aws_path=>“b”) would make an s3 object named foo.txt in bucket/b

Parameters:

  • local_file_path (String)
    • Location of file to put

  • aws_path (String)
    • S3 path to put the file

  • options (Hash) (defaults to: {})


227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/aws_region.rb', line 227

def put(local_file_path, aws_path, options={})
  aws_path = aws_path[0..-2] if aws_path[-1..-1] == '/'
  s3_path = "#{aws_path}/#{File.basename(local_file_path)}"
  log "s3 writing #{local_file_path} to bucket #{@id} path: #{aws_path} s3 path: #{s3_path}"
  f = File.open local_file_path, 'rb'
  options[:bucket] = @id
  options[:key] = s3_path
  options[:body] = f
  options[:storage_class] = 'REDUCED_REDUNDANCY'
  result = @region.s3.put_object(params=options)
  f.close
  result
end

#put_file(filename, file_identity) ⇒ AwsPageableResponse

Put a local file to this bucket

Parameters:

  • filename (String)
    • local file name

  • file_identity (String)
    • S3 file path

Returns:

  • (AwsPageableResponse)


210
211
212
213
214
215
216
217
218
219
# File 'lib/aws_region.rb', line 210

def put_file(filename, file_identity)
  File.open(filename, 'r') do |reading_file|
    resp = @region.s3.put_object(
        acl: "bucket-owner-full-control",
        body: reading_file,
        bucket: @id,
        key: file_identity
    )
  end
end