Module: QcloudCos::ConvenientApi

Included in:
QcloudCos
Defined in:
lib/qcloud_cos/convenient_api.rb

Instance Method Summary collapse

Instance Method Details

#bucket_info(bucket_name = nil) ⇒ Hash

获取 Bucket 信息

Parameters:

  • bucket_name (String) (defaults to: nil)

    :bucket (config.bucket) 指定当前 bucket, 默认是配置里面的 bucket

Returns:

  • (Hash)

    返回 Bucket 信息



9
10
11
12
13
14
# File 'lib/qcloud_cos/convenient_api.rb', line 9

def bucket_info(bucket_name = nil)
  bucket_name = bucket_name || config.bucket
  stat('/', bucket: bucket_name)['data']
rescue
  {}
end

#contains_file?(path = '/', options = {}) ⇒ Boolean

判断该路径下是否有文件

Parameters:

  • path (String) (defaults to: '/')

    指定路径

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

    额外参数

Options Hash (options):

  • :bucket (String) — default: config.bucket

    指定当前 bucket, 默认是配置里面的 bucket

Returns:

  • (Boolean)


52
53
54
# File 'lib/qcloud_cos/convenient_api.rb', line 52

def contains_file?(path = '/', options = {})
  !count(path, options)[:file_count].zero?
end

#contains_folder?(path = '/', options = {}) ⇒ Boolean

判断该路径下是否有文件夹

Parameters:

  • path (String) (defaults to: '/')

    指定路径

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

    额外参数

Options Hash (options):

  • :bucket (String) — default: config.bucket

    指定当前 bucket, 默认是配置里面的 bucket

Returns:

  • (Boolean)


63
64
65
# File 'lib/qcloud_cos/convenient_api.rb', line 63

def contains_folder?(path = '/', options = {})
  !count(path, options)[:folder_count].zero?
end

#count(path = '/', options = {}) ⇒ Hash

返回该路径下文件和文件夹的数目

Examples:

QcloudCos.count('/path/to/folder/') #=> { folder_count: 100, file_count: 1000 }

Parameters:

  • path (String) (defaults to: '/')

    指定路径

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

    额外参数

Options Hash (options):

  • :bucket (String) — default: config.bucket

    指定当前 bucket, 默认是配置里面的 bucket

Returns:

  • (Hash)


26
27
28
29
30
31
32
# File 'lib/qcloud_cos/convenient_api.rb', line 26

def count(path = '/', options = {})
  result = list_folders(path, options.merge(num: 1))
  {
    folder_count: result.dircount || 0,
    file_count: result.filecount || 0
  }
end

#empty?(path = '/', options = {}) ⇒ Boolean

判断该路径下是否为空

Parameters:

  • path (String) (defaults to: '/')

    指定路径

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

    额外参数

Options Hash (options):

  • :bucket (String) — default: config.bucket

    指定当前 bucket, 默认是配置里面的 bucket

Returns:

  • (Boolean)


41
42
43
# File 'lib/qcloud_cos/convenient_api.rb', line 41

def empty?(path = '/', options = {})
  count(path, options).values.uniq == 0
end

#exists?(path = '/', options = {}) ⇒ Boolean Also known as: exist?

判断文件或者文件夹是否存在

Parameters:

  • path (String) (defaults to: '/')

    指定文件路径

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

    额外参数

Options Hash (options):

  • :bucket (String) — default: config.bucket

    指定当前 bucket, 默认是配置里面的 bucket

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/qcloud_cos/convenient_api.rb', line 74

def exists?(path = '/', options = {})
  return true if path == '/' || path.to_s.empty?
  result = stat(path, options)
  result.key?('data') && result['data'].key?('name')
rescue
  false
end

#public_url(path, options = {}) ⇒ String

获取文件外网访问地址

Parameters:

  • path (String)

    指定文件路径

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

    额外参数

Options Hash (options):

  • :bucket (String) — default: config.bucket_name

    指定当前 bucket, 默认是配置里面的 bucket

  • :expired (Integer) — default: 600

    指定有效期, 秒为单位

Returns:

  • (String)

    下载地址

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/qcloud_cos/convenient_api.rb', line 94

def public_url(path, options = {})
  path = fixed_path(path)
  bucket = validates(path, options)

  result = stat(path, options)
  if result.key?('data') && result['data'].key?('access_url')
    expired = options['expired'] || PUBLIC_EXPIRED_SECONDS
    sign = authorization.sign(bucket, expired)
    "#{result['data']['access_url']}?sign=#{sign}"
  else
    fail FileNotExistError
  end
end