Class: Kozeki::Aws::S3Filesystem

Inherits:
Object
  • Object
show all
Includes:
Filesystem
Defined in:
lib/kozeki/aws/s3_filesystem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: ::Aws::S3::Client.new, bucket:, prefix:, delimiter: '/', cache_control: nil) ⇒ S3Filesystem

Returns a new instance of S3Filesystem.



11
12
13
14
15
16
17
# File 'lib/kozeki/aws/s3_filesystem.rb', line 11

def initialize(client: ::Aws::S3::Client.new, bucket:, prefix:, delimiter: '/', cache_control: nil)
  @s3 = client
  @bucket = bucket
  @prefix = prefix
  @delimiter = delimiter
  @cache_control = cache_control || method(:default_cache_control)
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



19
20
21
# File 'lib/kozeki/aws/s3_filesystem.rb', line 19

def bucket
  @bucket
end

#delimiterObject (readonly)

Returns the value of attribute delimiter.



19
20
21
# File 'lib/kozeki/aws/s3_filesystem.rb', line 19

def delimiter
  @delimiter
end

#prefixObject (readonly)

Returns the value of attribute prefix.



19
20
21
# File 'lib/kozeki/aws/s3_filesystem.rb', line 19

def prefix
  @prefix
end

#s3Object (readonly)

Returns the value of attribute s3.



19
20
21
# File 'lib/kozeki/aws/s3_filesystem.rb', line 19

def s3
  @s3
end

Instance Method Details

#delete(path) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/kozeki/aws/s3_filesystem.rb', line 46

def delete(path)
  @s3.delete_object(
    bucket:,
    key: make_key(path),
  )
  nil
rescue Aws::S3::Errors::NotFound
  nil
end

#list_entriesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kozeki/aws/s3_filesystem.rb', line 56

def list_entries
  @s3.list_objects_v2(
    bucket:,
    prefix:,
  ).flat_map do |page|
    page.contents.map do |content|
      Kozeki::Filesystem::Entry.new(
        path: key_to_path(content.key),
        mtime: content.last_modified,
      )
    end
  end
end

#read_with_mtime(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kozeki/aws/s3_filesystem.rb', line 21

def read_with_mtime(path)
  r = @s3.get_object(
    bucket:,
    key: make_key(path),
  )
  [
    r.body,
    r.last_modified,
  ]
rescue Aws::S3::Errors::NotFound
  raise Kozeki::Filesystem::NotFound
end

#write(path, string) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kozeki/aws/s3_filesystem.rb', line 34

def write(path, string)
  key = make_key(path)
  @s3.put_object(
    bucket:,
    key:,
    body: StringIO.new(string, 'r'), # avoid body to be logged
    content_type: content_type_for(path),
    cache_control: @cache_control[key],
  )
  nil
end