Class: Swineherd::S3FileSystem::S3File

Inherits:
Object
  • Object
show all
Defined in:
lib/swineherd-fs/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



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/swineherd-fs/s3filesystem.rb', line 255

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.



250
251
252
# File 'lib/swineherd-fs/s3filesystem.rb', line 250

def fs
  @fs
end

#handleObject

Returns the value of attribute handle.



250
251
252
# File 'lib/swineherd-fs/s3filesystem.rb', line 250

def handle
  @handle
end

#pathObject

Returns the value of attribute path.



250
251
252
# File 'lib/swineherd-fs/s3filesystem.rb', line 250

def path
  @path
end

Instance Method Details

#closeObject



298
299
300
301
302
303
304
305
306
# File 'lib/swineherd-fs/s3filesystem.rb', line 298

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

#readObject

Faster than iterating



274
275
276
277
# File 'lib/swineherd-fs/s3filesystem.rb', line 274

def read
  bucket,key = fs.split_path(path)
  fs.s3.interface.get_object(bucket, key)
end

#readlineObject

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



283
284
285
286
287
288
289
290
291
292
# File 'lib/swineherd-fs/s3filesystem.rb', line 283

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

#write(string) ⇒ Object



294
295
296
# File 'lib/swineherd-fs/s3filesystem.rb', line 294

def write string
  @handle.write(string)
end