Class: COS::Config

Inherits:
Struct::Base show all
Defined in:
lib/cos/config.rb

Constant Summary collapse

DEFAULT_HOST =

默认服务HOST

'web.file.myqcloud.com'
DEFAULT_MULTIPLE_SIGN_EXPIRE =

默认多次签名过期时间(单位秒)

600

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Struct::Base::AttrHelper

#optional_attrs, #required_attrs

Constructor Details

#initialize(options = {}) ⇒ COS::Config

初始化

Examples:

COS::Config.new(app_id: '', secret_id: '', secret_key: '')

Parameters:

  • options (Hash) (defaults to: {})

    客户端配置

Options Hash (options):

  • :app_id (String)

    COS分配的app_id

  • :secret_id (String)

    COS分配的secret_id

  • :secret_key (String)

    COS分配的secret_key

  • :host (String)

    COS服务host地址

  • :protocol (String)

    使用协议,默认为http,可选https

  • :open_timeout (Integer)

    接口通讯建立连接超时秒数

  • :read_timeout (Integer)

    接口通讯读取数据超时秒数

  • :config (String)

    配置文件路径

  • :log_src (String|Object)

    日志输出 STDOUT | STDERR | 'path/filename.log'

  • :log_level (Logger)

    日志级别 Logger::DEBUG | Logger::INFO | Logger::ERROR | Logger::FATAL

  • :multiple_sign_expire (String)

    多次签名过期时间(单位秒)

  • :default_bucket (String)

    默认bucket

Raises:

  • (AttrError)

    如果缺少参数会抛出参数错误异常


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cos/config.rb', line 48

def initialize(options = {})
  # 从配置文件加载配置
  if options[:config]
    config = load_config_file(options[:config])
    options.merge!(config)
  end

  super(options)

  # log_src: STDOUT | STDERR | 'path/filename.log'
  # log_level: Logger::DEBUG | Logger::INFO | Logger::ERROR | Logger::FATAL
  if options[:log_src]
    Logging.set_logger(
        options[:log_src],
        options[:log_level] || Logger::INFO
    )
  end

  @protocol ||= 'http'
  @host     ||= DEFAULT_HOST
  @api_base = "#{@protocol}://#{@host}/files/v1"
  @multiple_sign_expire ||= DEFAULT_MULTIPLE_SIGN_EXPIRE
end

Instance Attribute Details

#api_baseObject (readonly)

Returns the value of attribute api_base


22
23
24
# File 'lib/cos/config.rb', line 22

def api_base
  @api_base
end

Instance Method Details

#get_bucket(custom_bucket) ⇒ String

获取指定的bucket或从config中获取默认bucket

Returns:

  • (String)

Raises:


79
80
81
82
83
84
85
# File 'lib/cos/config.rb', line 79

def get_bucket(custom_bucket)
  b = custom_bucket || default_bucket
  if b == nil
    raise ClientError, 'Bucket name must be set'
  end
  b
end