class Kayvee::Clients::S3

An s3 backed kv store

Constants

InvalidBucketError

Public Class Methods

new(options) click to toggle source
# File lib/kayvee/clients/s3.rb, line 15
def initialize(options)
  @options = options
  validate_options!

  @s3 = ::S3::Service.new(access_key_id: aws_access_key,
                            secret_access_key: aws_secret_key)
end

Public Instance Methods

clear() click to toggle source

Clear the store

@return [Bool] true if all items removed, else false

# File lib/kayvee/clients/s3.rb, line 67
def clear
  objects.map(&:destroy).reduce(&:&)
end
exists?(path) click to toggle source

Check if key exists in the store

@param [String] path the key path @return [Bool] true of false

# File lib/kayvee/clients/s3.rb, line 60
def exists?(path)
  get(path).exists?
end
read(path) click to toggle source

@param [String] path the path to read

@return [Stringnil] the read string or nil if key does not exist

# File lib/kayvee/clients/s3.rb, line 35
def read(path)
  begin
    value = nil
    obj =  get(path)
    value = obj.content if obj
    value
  rescue ::S3::Error::NoSuchKey => e
    nil
  end
end
size() click to toggle source

Get the size of the store

@return [Integer] size of store

# File lib/kayvee/clients/s3.rb, line 74
def size
  objects.count
end
url(path) click to toggle source

Gets an s3 url for the key

@param [String] path the path to read

@return [String] the url

# File lib/kayvee/clients/s3.rb, line 28
def url(path)
  get(path).temporary_url
end
write(path, value) click to toggle source

@param [String/IO] path the path to read @param [String] value the value to set

@return [Key] the modified key

# File lib/kayvee/clients/s3.rb, line 50
def write(path, value)
  obj = build(path)
  obj.content = value
  obj.save
end

Private Instance Methods

aws_access_key() click to toggle source
# File lib/kayvee/clients/s3.rb, line 110
def aws_access_key
  options[:aws_access_key]
end
aws_secret_key() click to toggle source
# File lib/kayvee/clients/s3.rb, line 114
def aws_secret_key
  options[:aws_secret_key]
end
bucket() click to toggle source
# File lib/kayvee/clients/s3.rb, line 92
def bucket
  if bucket = @s3.buckets.find(bucket_name)
    bucket
  else
    raise InvalidBucketError, "bucket #{bucket_name} is invalid"
  end
end
bucket_name() click to toggle source
# File lib/kayvee/clients/s3.rb, line 106
def bucket_name
  options[:bucket_name]
end
build(path) click to toggle source
# File lib/kayvee/clients/s3.rb, line 80
def build(path)
  objects.build(path)
end
get(path) click to toggle source
# File lib/kayvee/clients/s3.rb, line 84
def get(path)
  objects.find(path)
end
objects() click to toggle source
# File lib/kayvee/clients/s3.rb, line 88
def objects
  bucket.objects
end
validate_options!() click to toggle source
# File lib/kayvee/clients/s3.rb, line 100
def validate_options!
  raise OptionMissingError.new, 'missing aws key' unless aws_access_key
  raise OptionMissingError.new, 'missing aws secret' unless aws_secret_key
  raise OptionMissingError.new, 'missing bucket name' unless bucket_name
end