Class: S3reamer::S3WriteStream

Inherits:
Object
  • Object
show all
Defined in:
lib/s3reamer/s3_write_stream.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  chunk_size: 5 * 1024 * 1024
}

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ S3WriteStream

Returns a new instance of S3WriteStream.



7
8
9
10
11
12
# File 'lib/s3reamer/s3_write_stream.rb', line 7

def initialize(object, options = {})
  @buffer = String.new
  @options = DEFAULT_OPTIONS.merge(options)
  @multipart_upload = object.initiate_multipart_upload
  @closed = false
end

Instance Method Details

#closeObject



27
28
29
30
31
# File 'lib/s3reamer/s3_write_stream.rb', line 27

def close
  flush unless @buffer.empty?
  @multipart_upload.complete(compute_parts: true)
  @closed = true
end

#flushObject



21
22
23
24
25
# File 'lib/s3reamer/s3_write_stream.rb', line 21

def flush
  part = @multipart_upload.part(@multipart_upload.parts.count + 1)
  part.upload(body: @buffer)
  @buffer.clear
end

#write(data) ⇒ Object

Raises:

  • (RuntimeError)


14
15
16
17
18
19
# File 'lib/s3reamer/s3_write_stream.rb', line 14

def write(data)
  raise RuntimeError.new("Illegal state: cannot write after close.") if @closed

  @buffer << data
  flush if @buffer.length >= @options[:chunk_size]
end