class Fluoride::Collector::Storage::S3

Attributes

response[R]

Public Instance Methods

access_secret() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 45
def access_secret
  @config.access_secret
end
authorization() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 81
def authorization
  hmac = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, access_secret, string_to_sign)
  signature = base64(hmac)

  "AWS #{key_id}:#{signature}"
end
base64(data) click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 73
def base64(data)
  if Base64.respond_to?(:strict_encode64)
    Base64.strict_encode64(data)
  else
    Base64.encode64(data).sub(/\s*\z/m,'')
  end
end
bucket() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 37
def bucket
  @config.bucket
end
content_md5() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 92
def content_md5
  @context_md5 ||= base64(OpenSSL::Digest::MD5.digest(record_yaml))
end
content_type() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 96
def content_type
  "text/yaml"
end
date() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 100
def date
  @date ||= Time.now.strftime("%a, %d %h %Y %T %z")
end
host() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 49
def host
  "#{bucket}.s3.amazonaws.com"
end
http() click to toggle source

:nocov:

# File lib/fluoride-collector/storage/s3.rb, line 26
def http
  @http ||=
    begin
      http = Net::HTTP.new(host, port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.ca_file = File.expand_path("../../../../certs/ca-certificates.crt", __FILE__)
      http
    end
end
key_id() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 41
def key_id
  @config.key_id
end
port() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 53
def port
  443
end
put_request() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 104
def put_request
  @req ||=
    begin
      req = Net::HTTP::Put.new(uri)
      req["Authorization"] = authorization
      req["Date"] = date
      req["Content-MD5"] = content_md5
      req["Content-Type"] = content_type
      req.body = record_yaml
      req
    end
end
remote_path() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 65
def remote_path
  @remote_path ||= "#{collection_type}-#{Process.pid}-#{Thread.current.object_id}-#{request_index}.yml"
end
request_index() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 57
def request_index
  @request_index ||=
    begin
      thread_locals[:request_index] ||= 0
      thread_locals[:request_index] += 1
    end
end
string_to_sign() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 88
def string_to_sign
  "PUT\n#{content_md5}\n#{content_type}\n#{date}\n/#{bucket}/#{remote_path}"
end
uri() click to toggle source
# File lib/fluoride-collector/storage/s3.rb, line 69
def uri
  "https://#{host}/#{remote_path}"
end
write() click to toggle source

:nocov:

# File lib/fluoride-collector/storage/s3.rb, line 16
def write
  http.start do
    @response = http.request(put_request)
  end
rescue
  $stderr.puts http.ca_file
  raise
end