Class: Attachie::S3MultipartUpload

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/attachie/s3_driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(s3_client, name, bucket, options, &block) ⇒ S3MultipartUpload

Returns a new instance of S3MultipartUpload.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/attachie/s3_driver.rb', line 9

def initialize(s3_client, name, bucket, options, &block)
  super()

  @s3_client = s3_client
  @bucket = bucket
  @name = name

  @parts = []

  @upload_id = @s3_client.create_multipart_upload(options.merge(bucket: bucket, key: name)).to_h[:upload_id]

  if block_given?
    begin
      block.call(self)
    rescue => e
      abort_upload

      raise e
    end

    complete_upload
  end
end

Instance Method Details

#abort_uploadObject



45
46
47
# File 'lib/attachie/s3_driver.rb', line 45

def abort_upload
  @s3_client.abort_multipart_upload(bucket: @bucket, key: @name, upload_id: @upload_id)
end

#complete_uploadObject



49
50
51
# File 'lib/attachie/s3_driver.rb', line 49

def complete_upload
  @s3_client.complete_multipart_upload(bucket: @bucket, key: @name, upload_id: @upload_id, multipart_upload: { parts: @parts })
end

#upload_part(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/attachie/s3_driver.rb', line 33

def upload_part(data)
  index = synchronize do
    part_number = @parts.size + 1

    @parts << { part_number: part_number, etag: "\"#{Digest::MD5.hexdigest(data)}\"" }

    part_number
  end

  @s3_client.upload_part(body: data, bucket: @bucket, key: @name, upload_id: @upload_id, part_number: index)
end