Class: COS::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/cos/signature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Signature

初始化

Examples:

COS::Signature.new(config)

Parameters:

See Also:



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

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/cos/signature.rb', line 12

def config
  @config
end

#expire_secondsObject (readonly)

Returns the value of attribute expire_seconds.



12
13
14
# File 'lib/cos/signature.rb', line 12

def expire_seconds
  @expire_seconds
end

#file_idObject (readonly)

Returns the value of attribute file_id.



12
13
14
# File 'lib/cos/signature.rb', line 12

def file_id
  @file_id
end

Instance Method Details

#multiple(bucket, expire_seconds = config.multiple_sign_expire) ⇒ String

Note:

用于上传,查询目录、文件,查询目录、文件,创建目录,下载(开启token防盗链)

多次有效签名

Parameters:

  • bucket (String)

    bucket名称

  • expire_seconds (Integer) (defaults to: config.multiple_sign_expire)

    签名有效时间(单位秒)

Returns:

  • (String)

    签名字符串

See Also:



59
60
61
62
63
64
65
66
67
68
# File 'lib/cos/signature.rb', line 59

def multiple(bucket, expire_seconds = config.multiple_sign_expire)
  # 多次签名时,过期时间应大于当前时间
  if expire_seconds <= 0
    raise AttrError, 'Multiple signature expire_seconds must greater than 0'
  end

  @expire_seconds = expire_seconds

  sign(:multiple, bucket)
end

#once(bucket, path) ⇒ String

Note:

用于删除目录、文件, 更新目录、文件

单次有效签名

Parameters:

  • bucket (String)

    bucket名称

  • path (String)

    文件或目录路径, 如 /path/, /path/file

Returns:

  • (String)

    签名字符串

See Also:



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cos/signature.rb', line 36

def once(bucket, path)
  # 单次签名,需要指定资源存储的唯一标识
  unless path.start_with?('/')
    path = "/#{path}"
  end

  # 资源存储的唯一标识,格式为"/app_id/bucket/用户自定义路径或资源名"
  @file_id = "/#{config.app_id}/#{bucket}#{path}"

  sign(:once, bucket)
end