class AwsRegion::AwsBucket

Methods for dealing with S3 buckets

Attributes

region[RW]

Public Class Methods

new(region, options={}) click to toggle source

Constructs a bucket instance from an existing bucket, or creates a new one with the name @param region [String] - Value from REGION static hash @param options [Hash] - Possible values:

  • :id - id of existing bucket

  • :bucket - Name of bucket to find or create

# 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

Public Instance Methods

delete() click to toggle source

Delete this bucket instance @return [AwsPageableResponse]]

# File lib/aws_region.rb, line 202
def delete
  @region.s3.delete_bucket({bucket: @id})
end
delete_all_objects() click to toggle source

delete all objects in a bucket @return [Boolean]

# 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={}) click to toggle source

deletes from s3 an object at :s3_path_to_object @param options [Hash] - Can be:

  • :s3_path_to_object

@return [Boolean]

# 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={}) click to toggle source

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.*

@param options [Hash] - Can contain:

  • :aws_path - first part of S3 path to search

  • :prefix - Actually suffix of path to search.

@return [Array<Hash>] - 0 or more objects

# 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={}) click to toggle source

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

@param options [Hash] - Can contain:

  • :s3_path_to_object - S3 object path

  • :dest_file_path - local file were file will be written

@return [Boolean]

# 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={}) click to toggle source

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 @param local_file_path [String] - Location of file to put @param aws_path [String] - S3 path to put the file @param options [Hash] - Can contain any valid S3 bucket options see [docs](docs.aws.amazon.com/sdkforruby/api/frames.html)

# 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) click to toggle source

Put a local file to this bucket @param filename [String] - local file name @param file_identity [String] - S3 file path @return [AwsPageableResponse]

# 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