module AliyunSDK::OSS::Util

Util functions to help generate formatted Date, signatures, etc.

Constants

HEADER_PREFIX

Prefix for OSS specific HTTP headers

Public Class Methods

get_content_md5(content) click to toggle source

Calculate content md5

# File lib/aliyun_sdk/oss/util.rb, line 58
def get_content_md5(content)
  Base64.strict_encode64(OpenSSL::Digest::MD5.digest(content))
end
get_signature(key, verb, headers, resources) click to toggle source

Calculate request signatures

# File lib/aliyun_sdk/oss/util.rb, line 23
def get_signature(key, verb, headers, resources)
  logger.debug("Sign, headers: #{headers}, resources: #{resources}")

  content_md5 = headers['content-md5'] || ""
  content_type = headers['content-type'] || ""
  date = headers['date']

  cano_headers = headers.select { |k, v| k.start_with?(HEADER_PREFIX) }
                 .map { |k, v| [k.downcase.strip, v.strip] }
                 .sort.map { |k, v| [k, v].join(":") + "\n" }.join

  cano_res = resources[:path] || "/"
  sub_res = (resources[:sub_res] || {})
            .sort.map { |k, v| v ? [k, v].join("=") : k }.join("&")
  cano_res += "?#{sub_res}" unless sub_res.empty?

  string_to_sign =
    "#{verb}\n#{content_md5}\n#{content_type}\n#{date}\n" +
    "#{cano_headers}#{cano_res}"

  Util.sign(key, string_to_sign)
end
sign(key, string_to_sign) click to toggle source

Sign a string using HMAC and BASE64 @param [String] key the secret key @param [String] string_to_sign the string to sign @return [String] the signature

# File lib/aliyun_sdk/oss/util.rb, line 50
def sign(key, string_to_sign)
  logger.debug("String to sign: #{string_to_sign}")

  Base64.strict_encode64(
    OpenSSL::HMAC.digest('sha1', key, string_to_sign))
end
symbolize(v) click to toggle source

Symbolize keys in Hash, recursively

# File lib/aliyun_sdk/oss/util.rb, line 63
def symbolize(v)
  if v.is_a?(Hash)
    result = {}
    v.each_key { |k| result[k.to_sym] = symbolize(v[k]) }
    result
  elsif v.is_a?(Array)
    result = []
    v.each { |x| result << symbolize(x) }
    result
  else
    v
  end
end