class Sgfa::StoreS3

Stores copies of {History}, {Entry}, and attached files that are in a {Jacket} using AWS S3.

Public Instance Methods

close() click to toggle source

Close the store

# File lib/sgfa/store_s3.rb, line 40
def close()
  true
end
delete(type, item) click to toggle source

Delete

# File lib/sgfa/store_s3.rb, line 98
def delete(type, item)
  key = _key(type, item)
  @s3.delete_object( bucket: @bck, key: key )
  return true
rescue Aws::S3::Errors::NoSuchKey
  return false
end
open(client, bucket, prefix=nil) click to toggle source

Open the store

@param client [AWS::S3::Client] The configured S3 client @param bucket [String] The bucket name @param prefix [String] Prefix to use for object keys

# File lib/sgfa/store_s3.rb, line 31
def open(client, bucket, prefix=nil)
  @s3 = client
  @bck = bucket
  @pre = prefix || ''
end
read(type, item) click to toggle source

Read an item from the store

# File lib/sgfa/store_s3.rb, line 69
def read(type, item)
  key = _key(type, item)
  fi = temp
  fi.set_encoding(Encoding::ASCII_8BIT)
  @s3.get_object( bucket: @bck, key: key, response_target: fi )
  fi.rewind
  return fi
rescue Aws::S3::Errors::NoSuchKey
  return false  
end
size(type, item) click to toggle source

Get size of an item

# File lib/sgfa/store_s3.rb, line 109
def size(type, item)
  key = _key(type, item)
  resp = @s3.head_object( bucket: @bck, key: key )
  return resp.content_length
rescue Aws::S3::Errors::NotFound
  return false
end
temp() click to toggle source

Get a temp file

# File lib/sgfa/store_s3.rb, line 47
def temp
  Tempfile.new('blob', Dir.tmpdir, :encoding => 'utf-8')
end
write(type, item, cont) click to toggle source

Store an item

# File lib/sgfa/store_s3.rb, line 83
def write(type, item, cont)
  key = _key(type, item)
  cont.rewind
  @s3.put_object( bucket: @bck, key: key, body: cont )

  if cont.respond_to?( :close! )
    cont.close!
  else
    cont.close
  end
end

Private Instance Methods

_key(type, item) click to toggle source

Get key

# File lib/sgfa/store_s3.rb, line 54
def _key(type, item)
  case type
    when :entry then ext = '-e'
    when :history then ext = '-h'
    when :file then ext = '-f'
    else raise NotImplementedError, 'Invalid item type'
  end
  key = @pre + item + ext
  return key
end