class Attachie::S3MultipartUpload

Public Class Methods

new(s3_client, name, bucket, options, &block) click to toggle source
Calls superclass method
# 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

Public Instance Methods

abort_upload() click to toggle source
# 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_upload() click to toggle source
# 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) click to toggle source
# 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