Class: Dockly::S3Writer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Util::Logger::Mixin
Defined in:
lib/dockly/s3_writer.rb

Constant Summary collapse

MAX_BUFFER_SIZE =
5 * 1024 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, s3_bucket, s3_object) ⇒ S3Writer

Returns a new instance of S3Writer.



13
14
15
16
17
18
19
20
# File 'lib/dockly/s3_writer.rb', line 13

def initialize(connection, s3_bucket, s3_object)
  @connection = connection
  @s3_bucket = s3_bucket
  @s3_object = s3_object
  @parts = []
  @closed = false
  @buffer = StringIO.new
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



8
9
10
# File 'lib/dockly/s3_writer.rb', line 8

def buffer
  @buffer
end

#closedObject (readonly)

Returns the value of attribute closed.



8
9
10
# File 'lib/dockly/s3_writer.rb', line 8

def closed
  @closed
end

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/dockly/s3_writer.rb', line 8

def connection
  @connection
end

#partsObject (readonly)

Returns the value of attribute parts.



8
9
10
# File 'lib/dockly/s3_writer.rb', line 8

def parts
  @parts
end

#s3_bucketObject (readonly)

Returns the value of attribute s3_bucket.



8
9
10
# File 'lib/dockly/s3_writer.rb', line 8

def s3_bucket
  @s3_bucket
end

#s3_objectObject (readonly)

Returns the value of attribute s3_object.



8
9
10
# File 'lib/dockly/s3_writer.rb', line 8

def s3_object
  @s3_object
end

Instance Method Details

#abort_unless_closedObject



69
70
71
72
# File 'lib/dockly/s3_writer.rb', line 69

def abort_unless_closed
  abort_upload unless @closed
  @closed = true
end

#abort_uploadObject



61
62
63
64
65
66
67
# File 'lib/dockly/s3_writer.rb', line 61

def abort_upload
  connection.abort_multipart_upload(
    bucket: s3_bucket,
    key: s3_object,
    upload_id: upload_id
  )
end

#closeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dockly/s3_writer.rb', line 42

def close
  return if @closed
  upload_buffer unless buffer.size.zero?
  connection.complete_multipart_upload(
    bucket: s3_bucket,
    key: s3_object,
    upload_id: upload_id,
    multipart_upload: {
      parts: @parts.each_with_index.map do |part, idx|
        {
          etag: part,
          part_number: idx.succ
        }
      end
    }
  )
  @closed = true
end

#multipart_uploadObject



74
75
76
77
78
79
80
# File 'lib/dockly/s3_writer.rb', line 74

def multipart_upload
  @multipart_upload ||= connection.create_multipart_upload(
    bucket: s3_bucket,
    key: s3_object,
    acl: 'bucket-owner-full-control',
  )
end

#upload_bufferObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dockly/s3_writer.rb', line 22

def upload_buffer
  num = @parts.length.succ
  debug "Writing chunk ##{num} to s3://#{s3_bucket}/#{s3_object} with upload id: #{upload_id}"
  res = connection.upload_part(
    bucket: s3_bucket,
    key: s3_object,
    upload_id: upload_id,
    part_number:num,
    body: buffer.tap(&:rewind)
  )
  @parts << res.etag
  @buffer = StringIO.new
end

#write(chunk) ⇒ Object



36
37
38
39
40
# File 'lib/dockly/s3_writer.rb', line 36

def write(chunk)
  @buffer.write(chunk)
  upload_buffer if buffer.size > MAX_BUFFER_SIZE
  chunk.length
end