Module: QcloudCos::ConvenientApi

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

Instance Method Summary collapse

Instance Method Details

#all(path, options = {}) ⇒ Hash

列出所有文件或者目录

Parameters:

  • path (String)

    指定目标路径, 以 / 结尾, 则列出该目录下文件或者文件夹,不以 / 结尾,就搜索该前缀的文件或者文件夹

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

    额外参数

Options Hash (options):

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

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

  • :pattern (String) — default: eListBoth

    指定拉取的内容,可选值: eListBoth, eListDirOnly, eListFileOnly

  • :order (Integer) — default: 0

    指定拉取文件的顺序, 默认为正序(=0), 可选值: 0, 1

Returns:

  • (Hash)


116
117
118
119
120
121
122
123
124
125
# File 'lib/qcloud_cos/convenient_api.rb', line 116

def all(path, options = {})
  results = []
  loop do
    objects = QcloudCos.list(path, options)
    results += objects.to_a
    break unless objects.has_more
    options['context'] = objects.context
  end
  results
end

#bucket_info(bucket_name = nil) ⇒ Hash

获取 Bucket 信息

Parameters:

  • bucket_name (String) (defaults to: nil)

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

Returns:

  • (Hash)

    返回 Bucket 信息



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

def bucket_info(bucket_name = nil)
  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)


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

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)


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

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)


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

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)


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

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)


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

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:



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

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