Class: S3io::WriteWrapper

Inherits:
Wrapper
  • Object
show all
Defined in:
lib/s3io/write_wrapper.rb

Constant Summary collapse

MAX_FILE_SIZE =

Default maximum file size

1 * 1024 * 1024 * 1024 * 1024
MAX_NUM_CHUNKS =

S3 only supports up to 10K multipart chunks

10000
MIN_CHUNK_SIZE =

Minimum chunk size in S3 is 5MiB

5 * 1024 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s3object, options = {}) ⇒ WriteWrapper



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/s3io/write_wrapper.rb', line 21

def initialize(s3object, options = {})
  super(s3object)

  @options = {
    :max_file_size => (options[:max_file_size] || MAX_FILE_SIZE),
    :multipart_upload_options => (options[:multipart_upload_options] || {})
  }

  @min_chunk_size = [(@options[:max_file_size].to_f / MAX_NUM_CHUNKS).ceil, MIN_CHUNK_SIZE].max
  @multipart_upload = @s3object.multipart_upload(@options[:multipart_upload_options])
  @write_buffer = ''
end

Instance Attribute Details

#posObject (readonly)

Number of bytes written to S3



19
20
21
# File 'lib/s3io/write_wrapper.rb', line 19

def pos
  @pos
end

Instance Method Details

#closeObject



56
57
58
59
60
61
62
# File 'lib/s3io/write_wrapper.rb', line 56

def close
  self.flush
  @multipart_upload.close
  @multipart_upload = nil

  super
end

#flushObject



47
48
49
50
51
52
53
54
# File 'lib/s3io/write_wrapper.rb', line 47

def flush
  return if @write_buffer.empty?

  @multipart_upload.add_part(@write_buffer)
  @write_buffer.replace '' # String#clear isn't available on 1.8.x

  return nil
end

#write(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/s3io/write_wrapper.rb', line 34

def write(data)
  fail "S3 Object is already closed" unless @s3object

  data_str = data.to_s
  data_size = data_str.size

  @write_buffer << data_str
  @pos += data_size
  self.flush if @write_buffer.size >= @min_chunk_size

  return data_size
end