Class: Swineherd::S3FileSystem::S3File

Inherits:
Object
  • Object
show all
Defined in:
lib/swineherd/filesystem/s3filesystem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode, fs, &blk) ⇒ S3File

In order to open input and output streams we must pass around the s3 fs object itself



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 190

def initialize path, mode, fs, &blk
  @fs   = fs
  @path = path
  case mode
  when "r" then
    raise "#{fs.type(path)} is not a readable file - #{path}" unless fs.type(path) == "file"
  when "w" then
    raise "Path #{path} is a directory." unless (fs.type(path) == "file") || (fs.type(path) == "unknown")
    @handle = Tempfile.new('s3filestream')
    if block_given?
      yield self
      close
    end
  end
end

Instance Attribute Details

#fsObject

Returns the value of attribute fs.



185
186
187
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 185

def fs
  @fs
end

#handleObject

Returns the value of attribute handle.



185
186
187
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 185

def handle
  @handle
end

#pathObject

Returns the value of attribute path.



185
186
187
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 185

def path
  @path
end

Instance Method Details

#closeObject



236
237
238
239
240
241
242
243
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 236

def close
  if @handle
    @handle.read
    fs.s3.interface.put(fs.bucket(path), fs.key_path(path), File.open(@handle.path, 'r'))
    @handle.close
  end
  @handle = nil
end

#puts(string) ⇒ Object



232
233
234
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 232

def puts string
  write(string+"\n")
end

#readObject

Faster than iterating



209
210
211
212
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 209

def read
  resp = fs.s3.interface.get_object(fs.bucket(path), fs.key_path(path))
  resp
end

#readlineObject

This is a little hackety. That is, once you call (.each) on the object the full object starts downloading…



218
219
220
221
222
223
224
225
226
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 218

def readline
  @handle ||= fs.s3.interface.get_object(fs.bucket(path), fs.key_path(path)).each        
  begin
    @handle.next
  rescue StopIteration, NoMethodError
    @handle = nil
    raise EOFError.new("end of file reached")
  end
end

#write(string) ⇒ Object



228
229
230
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 228

def write string
  @handle.write(string)
end