class VzaarApi::Upload::S3

Constants

SEND_TIMEOUT

Attributes

attrs[R]
path[R]
signature[R]

Public Class Methods

new(attrs, signature) click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 14
def initialize(attrs, signature)
  @attrs = attrs.dup
  @path = @attrs.delete(:path)
  @signature = signature
end

Public Instance Methods

execute() click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 20
def execute
  if multipart?
    multipart_upload
  else
    single_part_upload
  end
  attrs.merge({ guid: guid })
end

Private Instance Methods

error_message(body) click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 82
def error_message(body)
  /Message\>(.+)\<\/Message/.match(body)[1]
end
headers() click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 61
def headers
  @headers ||= {
    'acl' => acl,
    'bucket' => bucket,
    'success_action_status' => success_action_status,
    'policy' => policy,
    'x-amz-meta-uploader' => UPLOADER
  }.merge! x_amz_headers
end
http_client() click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 71
def http_client
  @http_client ||= begin
    HTTPClient.new.tap { |c| c.send_timeout = SEND_TIMEOUT }
  end
end
multipart_upload() click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 31
def multipart_upload
  _headers = headers.dup
  chunk = 0
  File.open(path, "r") do |file|
    until file.eof?
      _headers['key'] = "#{key}.#{chunk}"
      _headers['file'] = VirtualFile.new(file, part_size_in_bytes)
      validate_response! upload_file(_headers)
      chunk += 1
    end
  end
rescue => e
  raise Error.new(e.message)
end
single_part_upload() click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 46
def single_part_upload
  File.open(path, "r") do |file|
    _headers = headers.dup
    _headers['key'] = key
    _headers['file'] = file
    validate_response! upload_file(_headers)
  end
rescue => e
  raise Error.new(e.message)
end
upload_file(headers) click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 57
def upload_file(headers)
  http_client.post upload_hostname, headers
end
validate_response!(response) click to toggle source
# File lib/vzaar_api/upload/s3.rb, line 77
def validate_response!(response)
  return if response.ok?
  raise error_message(response.body)
end