Module: Aliyun::OSS::Util

Extended by:
Logging
Defined in:
lib/aliyun/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 Logging

Logging::DEFAULT_LOG_FILE

Class Method Summary collapse

Methods included from Logging

logger, set_log_file, set_log_level

Class Method Details

.get_content_md5(content) ⇒ Object

Calculate content md5



58
59
60
# File 'lib/aliyun/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) ⇒ Object

Calculate request signatures



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/aliyun/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}"

  logger.debug("String to sign: #{string_to_sign}")

  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:



52
53
54
55
# File 'lib/aliyun/oss/util.rb', line 52

def sign(key, string_to_sign)
  Base64.strict_encode64(
    OpenSSL::HMAC.digest('sha1', key, string_to_sign))
end