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



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 228

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.



223
224
225
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 223

def fs
  @fs
end

#handleObject

Returns the value of attribute handle.



223
224
225
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 223

def handle
  @handle
end

#pathObject

Returns the value of attribute path.



223
224
225
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 223

def path
  @path
end

Instance Method Details

#closeObject



274
275
276
277
278
279
280
281
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 274

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



270
271
272
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 270

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

#readObject

Faster than iterating



247
248
249
250
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 247

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…



256
257
258
259
260
261
262
263
264
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 256

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



266
267
268
# File 'lib/swineherd/filesystem/s3filesystem.rb', line 266

def write string
  @handle.write(string)
end