Class: ICFS::StoreS3

Inherits:
Store
  • Object
show all
Defined in:
lib/icfs/store_s3.rb

Overview

Permanent store for items using AWS S3

Instance Method Summary collapse

Methods inherited from Store

#action_read, #action_write, #case_read, #case_write, #close, #entry_read, #entry_write, #index_read, #index_write, #log_read, #log_write

Constructor Details

#initialize(client, bucket, prefix = nil) ⇒ StoreS3

New store

Parameters:

  • client (Aws::S3::Client)

    The configured S3 client

  • bucket (String)

    The bucket name

  • prefix (String) (defaults to: nil)

    Prefix to use for object keys



32
33
34
35
36
# File 'lib/icfs/store_s3.rb', line 32

def initialize(client, bucket, prefix=nil)
  @s3 = client
  @bck = bucket
  @base = prefix || ''
end

Instance Method Details

#file_read(cid, enum, lnum, fnum) ⇒ File, Tempfile

Read a file

Parameters:

  • cid (String)

    caseid

  • enum (Integer)

    Entry number

  • lnum (Integer)

    Log number

  • fnum (Integer)

    File number

Returns:

  • (File, Tempfile)

    Read only copy of the file



42
43
44
45
46
47
48
49
50
# File 'lib/icfs/store_s3.rb', line 42

def file_read(cid, enum, lnum, fnum)
  tmp = tempfile
  key = _file(cid, enum, lnum, fnum)
  @s3.get_object( bucket: @bck, key: key, response_target: tmp)
  tmp.rewind
  return tmp
rescue Aws::S3::Errors::NoSuchKey
  return nil
end

#file_size(cid, enum, lnum, fnum) ⇒ Integer

Get a file size

Parameters:

  • cid (String)

    caseid

  • enum (Integer)

    Entry number

  • lnum (Integer)

    Log number

  • fnum (Integer)

    File number

Returns:

  • (Integer)

    The size of the file



71
72
73
74
75
76
77
# File 'lib/icfs/store_s3.rb', line 71

def file_size(cid, enum, lnum, fnum)
  key = _file(cid, enum, lnum, fnum)
  resp = @s3.head_object( bucket: @bck, key: key )
  return resp.content_length
rescue Aws::S3::Errors::NotFound
  return nil
end

#file_write(cid, enum, lnum, fnum, tmpf) ⇒ Object

Write a file

Parameters:

  • cid (String)

    caseid

  • enum (Integer)

    Entry number

  • lnum (Integer)

    Log number

  • fnum (Integer)

    File number

  • tmpf (Tempfile)

    A Tempfile obtained from #tempfile



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/icfs/store_s3.rb', line 56

def file_write(cid, enum, lnum, fnum, tmpf)
  key = _file(cid, enum, lnum, fnum)
  tmpf.rewind
  @s3.put_object( bucket: @bck, key: key, body: tmpf )

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

#tempfileTempfile

Get a Tempfile to use to write files

Returns:

  • (Tempfile)

    a Tempfile which can be written and passed to #file_write



83
84
85
# File 'lib/icfs/store_s3.rb', line 83

def tempfile
  Tempfile.new('tmp', encoding: 'ascii-8bit')
end