Class: Dockly::AWS::S3Writer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, s3_bucket, s3_object) ⇒ S3Writer

Returns a new instance of S3Writer.



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

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

Instance Attribute Details

#buffer ⇒ Object

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

#connection ⇒ Object (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#s3_bucket ⇒ Object (readonly)

Returns the value of attribute s3_bucket.



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

def s3_bucket
  @s3_bucket
end

#s3_object ⇒ Object (readonly)

Returns the value of attribute s3_object.



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

def s3_object
  @s3_object
end

#upload_id ⇒ Object (readonly)

Returns the value of attribute upload_id.



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

def upload_id
  @upload_id
end

Instance Method Details

#abort ⇒ Object



49
50
51
# File 'lib/dockly/aws/s3_writer.rb', line 49

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

#abort_unless_closed ⇒ Object



53
54
55
56
# File 'lib/dockly/aws/s3_writer.rb', line 53

def abort_unless_closed
  abort unless @closed
  @closed = true
end

#close ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/dockly/aws/s3_writer.rb', line 38

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

#upload_buffer ⇒ Object



23
24
25
26
27
28
# File 'lib/dockly/aws/s3_writer.rb', line 23

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

#write(chunk) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/dockly/aws/s3_writer.rb', line 30

def write(chunk)
  self.buffer << chunk

  upload_buffer if buffer.bytesize > 5242880

  chunk.length
end