Module: QcloudCos::ConvenientApi
- Included in:
- QcloudCos
- Defined in:
- lib/qcloud_cos/convenient_api.rb
Instance Method Summary collapse
-
#bucket_info(bucket_name = nil) ⇒ Hash
获取 Bucket 信息.
-
#contains_file?(path = '/', options = {}) ⇒ Boolean
判断该路径下是否有文件.
-
#contains_folder?(path = '/', options = {}) ⇒ Boolean
判断该路径下是否有文件夹.
-
#count(path = '/', options = {}) ⇒ Hash
返回该路径下文件和文件夹的数目.
-
#empty?(path = '/', options = {}) ⇒ Boolean
判断该路径下是否为空.
-
#exists?(path = '/', options = {}) ⇒ Boolean
(also: #exist?)
判断文件或者文件夹是否存在.
-
#public_url(path, options = {}) ⇒ String
获取文件外网访问地址.
Instance Method Details
#bucket_info(bucket_name = nil) ⇒ 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
判断该路径下是否有文件
52 53 54 |
# File 'lib/qcloud_cos/convenient_api.rb', line 52 def contains_file?(path = '/', = {}) !count(path, )[:file_count].zero? end |
#contains_folder?(path = '/', options = {}) ⇒ Boolean
判断该路径下是否有文件夹
63 64 65 |
# File 'lib/qcloud_cos/convenient_api.rb', line 63 def contains_folder?(path = '/', = {}) !count(path, )[:folder_count].zero? end |
#count(path = '/', options = {}) ⇒ Hash
返回该路径下文件和文件夹的数目
26 27 28 29 30 31 32 |
# File 'lib/qcloud_cos/convenient_api.rb', line 26 def count(path = '/', = {}) result = list_folders(path, .merge(num: 1)) { folder_count: result.dircount || 0, file_count: result.filecount || 0 } end |
#empty?(path = '/', options = {}) ⇒ Boolean
判断该路径下是否为空
41 42 43 |
# File 'lib/qcloud_cos/convenient_api.rb', line 41 def empty?(path = '/', = {}) count(path, ).values.uniq == 0 end |
#exists?(path = '/', options = {}) ⇒ Boolean Also known as: exist?
判断文件或者文件夹是否存在
74 75 76 77 78 79 80 |
# File 'lib/qcloud_cos/convenient_api.rb', line 74 def exists?(path = '/', = {}) return true if path == '/' || path.to_s.empty? result = stat(path, ) result.key?('data') && result['data'].key?('name') rescue false end |
#public_url(path, options = {}) ⇒ String
获取文件外网访问地址
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, = {}) path = fixed_path(path) bucket = validates(path, ) result = stat(path, ) if result.key?('data') && result['data'].key?('access_url') expired = ['expired'] || PUBLIC_EXPIRED_SECONDS sign = .sign(bucket, expired) "#{result['data']['access_url']}?sign=#{sign}" else fail FileNotExistError end end |