Module: Vos::Drivers::S3VfsStorage

Included in:
S3
Defined in:
lib/vos/drivers/s3_vfs_storage.rb

Defined Under Namespace

Classes: Error, Writer

Instance Method Summary collapse

Instance Method Details

#attributes(path) ⇒ Object

Attributes



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 21

def attributes path
  path = normalize_path(path)
  return {dir: true, file: false} if path.empty?

  file = bucket.objects[path]
  if file.exists?
    attrs = {}
    attrs[:file] = true
    attrs[:dir] = false
    attrs[:size] = file.content_length
    attrs[:updated_at] = file.last_modified
    attrs
  else
    # There's no dirs in S3, so we always return nil
    nil
  end
end

#create_dir(path) ⇒ Object

Dir



79
80
81
82
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 79

def create_dir path
  # there's no concept of dir in s
  # raise Error, ":create_dir not supported!"
end

#delete_dir(path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 84

def delete_dir path
  path = normalize_path path

  bucket.as_tree(prefix: path).children.each do |obj|
    if obj.branch?
      delete_dir "/#{obj.prefix}"
    elsif obj.leaf?
      bucket.objects[obj.key].delete
    else
      raise "unknow node type!"
    end
  end
end

#delete_file(path) ⇒ Object



69
70
71
72
73
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 69

def delete_file path
  path = normalize_path path
  file = bucket.objects[path]
  file.delete
end

#each_entry(path, query, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 98

def each_entry path, query, &block
  path = normalize_path path
  raise "S3 not support :each_entry with query!" if query

  bucket.as_tree(prefix: path).children.each do |obj|
    if obj.branch?
      block.call obj.prefix.sub("#{path}/", '').sub(/\/$/, ''), :dir
    elsif obj.leaf?
      block.call obj.key.sub("#{path}/", ''), :file
    else
      raise "unknow node type!"
    end
  end
end

#local?Boolean

Returns:

  • (Boolean)


129
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 129

def local?; false end

#read_file(path, &block) ⇒ Object

File



46
47
48
49
50
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 46

def read_file path, &block
  path = normalize_path path
  file = bucket.objects[path]
  block.call file.read
end

#set_attributes(path, attrs) ⇒ Object



39
40
41
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 39

def set_attributes path, attrs
  raise 'not supported'
end

#tmp(&block) ⇒ Object

Special



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 116

def tmp &block
  tmp_dir = "/tmp/#{rand(10**6)}"
  if block
    begin
      block.call tmp_dir
    ensure
      delete_dir tmp_dir
    end
  else
    tmp_dir
  end
end

#write_file(original_path, append, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vos/drivers/s3_vfs_storage.rb', line 52

def write_file original_path, append, &block
  path = normalize_path original_path

  file = bucket.objects[path]
  if append
    # there's no support for :append in Fog, so we just mimic it
    writer = Writer.new
    writer.write file.read if file.exists?
    block.call writer
    file.write writer.data, acl: acl
  else
    writer = Writer.new
    block.call writer
    file.write writer.data, acl: acl
  end
end