Class: Dockly::S3Writer
- Inherits:
-
Object
- Object
- Dockly::S3Writer
- 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
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#closed ⇒ Object
readonly
Returns the value of attribute closed.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#s3_bucket ⇒ Object
readonly
Returns the value of attribute s3_bucket.
-
#s3_object ⇒ Object
readonly
Returns the value of attribute s3_object.
Instance Method Summary collapse
- #abort_unless_closed ⇒ Object
- #abort_upload ⇒ Object
- #close ⇒ Object
-
#initialize(connection, s3_bucket, s3_object) ⇒ S3Writer
constructor
A new instance of S3Writer.
- #multipart_upload ⇒ Object
- #upload_buffer ⇒ Object
- #write(chunk) ⇒ Object
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
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
8 9 10 |
# File 'lib/dockly/s3_writer.rb', line 8 def buffer @buffer end |
#closed ⇒ Object (readonly)
Returns the value of attribute closed.
8 9 10 |
# File 'lib/dockly/s3_writer.rb', line 8 def closed @closed end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/dockly/s3_writer.rb', line 8 def connection @connection end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
8 9 10 |
# File 'lib/dockly/s3_writer.rb', line 8 def parts @parts end |
#s3_bucket ⇒ Object (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_object ⇒ Object (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_closed ⇒ Object
69 70 71 72 |
# File 'lib/dockly/s3_writer.rb', line 69 def abort_unless_closed abort_upload unless @closed @closed = true end |
#abort_upload ⇒ Object
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 |
#close ⇒ Object
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_upload ⇒ Object
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_buffer ⇒ Object
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 |