Sha256: 928cd87e3aa8498b0dd8e185476a3a0a855c1365c261842813a53e145175cde6

Contents?: true

Size: 1.49 KB

Versions: 33

Compression:

Stored size: 1.49 KB

Contents

module Dockly
  module AWS
    class S3Writer
      include Dockly::Util::Logger::Mixin

      logger_prefix '[dockly s3writer]'

      attr_accessor :buffer
      attr_reader :connection, :s3_bucket, :s3_object, :upload_id

      def initialize(connection, s3_bucket, s3_object)
        @connection = connection
        @s3_bucket = s3_bucket
        @s3_object = s3_object
        @parts = []
        @closed = false
        @buffer = ""

        init_upload_res = connection.initiate_multipart_upload(s3_bucket, s3_object)
        @upload_id = init_upload_res.body['UploadId']
      end

      def upload_buffer
        res = connection.upload_part(s3_bucket, s3_object, upload_id, @parts.size + 1, buffer)
        @parts << res.headers["ETag"]
        debug "Writing a chunk"
        @buffer = ""
      end

      def write(chunk)
        self.buffer << chunk

        upload_buffer if buffer.bytesize > 5242880

        chunk.length
      end

      def close
        return if @closed
        upload_buffer unless buffer.empty?

        res = connection.complete_multipart_upload(s3_bucket, s3_object, upload_id, @parts)
        if res.body['Code'] || res.body['Message']
          raise "Failed to upload to S3: #{res.body['Code']}: #{res.body['Message']}"
        end
        @closed = true
      end

      def abort
        connection.abort_multipart_upload(s3_bucket, s3_object, upload_id)
      end

      def abort_unless_closed
        abort unless @closed
        @closed = true
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
dockly-2.7.2 lib/dockly/aws/s3_writer.rb
dockly-2.7.1 lib/dockly/aws/s3_writer.rb
dockly-2.7.0 lib/dockly/aws/s3_writer.rb
dockly-1.13.0 lib/dockly/aws/s3_writer.rb
dockly-2.6.0 lib/dockly/aws/s3_writer.rb
dockly-1.12.3 lib/dockly/aws/s3_writer.rb
dockly-2.5.3 lib/dockly/aws/s3_writer.rb
dockly-1.12.2 lib/dockly/aws/s3_writer.rb
dockly-2.5.2 lib/dockly/aws/s3_writer.rb
dockly-1.12.1 lib/dockly/aws/s3_writer.rb
dockly-2.5.1 lib/dockly/aws/s3_writer.rb
dockly-1.12.0 lib/dockly/aws/s3_writer.rb
dockly-2.5.0 lib/dockly/aws/s3_writer.rb
dockly-1.11.0 lib/dockly/aws/s3_writer.rb
dockly-2.4.0 lib/dockly/aws/s3_writer.rb
dockly-2.3.0 lib/dockly/aws/s3_writer.rb
dockly-1.10.0 lib/dockly/aws/s3_writer.rb
dockly-1.9.0 lib/dockly/aws/s3_writer.rb
dockly-1.8.0 lib/dockly/aws/s3_writer.rb
dockly-2.2.0 lib/dockly/aws/s3_writer.rb