Module: AliyunSDK::OSS::Util

Extended by:
Common::Logging
Defined in:
lib/aliyun_sdk/oss/util.rb

Overview

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

Constant Summary collapse

HEADER_PREFIX =

Prefix for OSS specific HTTP headers

"x-oss-"

Constants included from Common::Logging

Common::Logging::DEFAULT_LOG_FILE, Common::Logging::MAX_NUM_LOG, Common::Logging::ROTATE_SIZE

Class Method Summary collapse

Methods included from Common::Logging

logger, set_log_file, set_log_level

Class Method Details

.get_content_md5(content) ⇒ Object

Calculate content md5



59
60
61
# File 'lib/aliyun_sdk/oss/util.rb', line 59

def get_content_md5(content)
  Base64.strict_encode64(OpenSSL::Digest::MD5.digest(content))
end

.get_signature(key, verb, headers, resources) ⇒ Object

Calculate request signatures



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/aliyun_sdk/oss/util.rb', line 24

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) ⇒ String

Sign a string using HMAC and BASE64

Parameters:

  • key (String)

    the secret key

  • string_to_sign (String)

    the string to sign

Returns:



51
52
53
54
55
56
# File 'lib/aliyun_sdk/oss/util.rb', line 51

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) ⇒ Object

Symbolize keys in Hash, recursively



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aliyun_sdk/oss/util.rb', line 64

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