Module: S3Multipart::TransferHelpers

Included in:
Upload
Defined in:
lib/s3_multipart/transfer_helpers.rb

Overview

Collection of methods to be mixed in to the Upload class.

Handle all communication with Amazon S3 servers

Instance Method Summary collapse

Instance Method Details

#complete(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/s3_multipart/transfer_helpers.rb', line 36

def complete(options)
  options[:content_type] = "application/xml"

  url = "/#{options[:object_name]}?uploadId=#{options[:upload_id]}"
  
  body = format_part_list_in_xml(options)
  headers = { content_type: options[:content_type],
              content_length: options[:content_length] }
            
  headers[:authorization], headers[:date] = sign_request verb: 'POST', url: url, content_type: options[:content_type]

  response = Http.post url, {headers: headers, body: body}
  parsed_response_body = XmlSimple.xml_in(response.body)  

  begin
    return { location: parsed_response_body["Location"][0] }
  rescue NoMethodError
    return { error: "Upload does not exist"} if parsed_response_body["Message"].first.match("The specified upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.")
  end
end

#initiate(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/s3_multipart/transfer_helpers.rb', line 7

def initiate(options)
  real_name = options[:object_name]
  unique_name = UUID.generate + real_name.match(/.[A-Za-z0-9]+$/)[0] # clean this up later
  url = "/#{unique_name}?uploads"

  headers = {content_type: options[:content_type]}
  headers[:authorization], headers[:date] = sign_request verb: 'POST', url: url, content_type: options[:content_type]

  response = Http.post url, {headers: headers}
  parsed_response_body = XmlSimple.xml_in(response.body)  

  return { "key"  => parsed_response_body["Key"][0],
           "upload_id"   => parsed_response_body["UploadId"][0],
           "name" => real_name }
end

#sign_batch(options) ⇒ Object



23
24
25
26
27
# File 'lib/s3_multipart/transfer_helpers.rb', line 23

def sign_batch(options)
  parts = options[:content_lengths].split('-').each_with_index.map do |len, i|
    sign_part(options.merge!({content_length: len, part_number: i+1}))
  end
end

#sign_part(options) ⇒ Object



29
30
31
32
33
34
# File 'lib/s3_multipart/transfer_helpers.rb', line 29

def sign_part(options)
  url = "/#{options[:object_name]}?partNumber=#{options[:part_number]}&uploadId=#{options[:upload_id]}"
  authorization, date = sign_request verb: 'PUT', url: url, content_length: options[:content_length]
  
  return {authorization: authorization, date: date}
end

#sign_request(options) ⇒ Object



57
58
59
60
61
62
# File 'lib/s3_multipart/transfer_helpers.rb', line 57

def sign_request(options)
  #options.default = ""
  time = Time.now.strftime("%a, %d %b %Y %T %Z")

  return [calculate_authorization_hash(time, options), time]
end