Module: QcloudCos::ConvenientApi
- Included in:
- QcloudCos
- Defined in:
- lib/qcloud_cos/convenient_api.rb
Instance Method Summary collapse
-
#all(path, options = {}) ⇒ Hash
列出所有文件或者目录.
-
#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
permalink #all(path, options = {}) ⇒ Hash
列出所有文件或者目录
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/qcloud_cos/convenient_api.rb', line 116 def all(path, = {}) results = [] loop do objects = QcloudCos.list(path, ) results += objects.to_a break unless objects.has_more ['context'] = objects.context end results end |
permalink #bucket_info(bucket_name = nil) ⇒ 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 |
permalink #contains_file?(path = '/', options = {}) ⇒ Boolean
判断该路径下是否有文件
51 52 53 |
# File 'lib/qcloud_cos/convenient_api.rb', line 51 def contains_file?(path = '/', = {}) !count(path, )[:file_count].zero? end |
permalink #contains_folder?(path = '/', options = {}) ⇒ Boolean
判断该路径下是否有文件夹
62 63 64 |
# File 'lib/qcloud_cos/convenient_api.rb', line 62 def contains_folder?(path = '/', = {}) !count(path, )[:folder_count].zero? end |
permalink #count(path = '/', options = {}) ⇒ Hash
返回该路径下文件和文件夹的数目
25 26 27 28 29 30 31 |
# File 'lib/qcloud_cos/convenient_api.rb', line 25 def count(path = '/', = {}) result = list_folders(path, .merge(num: 1)) { folder_count: result.dircount || 0, file_count: result.filecount || 0 } end |
permalink #empty?(path = '/', options = {}) ⇒ Boolean
判断该路径下是否为空
40 41 42 |
# File 'lib/qcloud_cos/convenient_api.rb', line 40 def empty?(path = '/', = {}) count(path, ).values.uniq == 0 end |
permalink #exists?(path = '/', options = {}) ⇒ Boolean Also known as: exist?
判断文件或者文件夹是否存在
73 74 75 76 77 78 79 |
# File 'lib/qcloud_cos/convenient_api.rb', line 73 def exists?(path = '/', = {}) return true if path == '/' || path.to_s.empty? result = stat(path, ) result.key?('data') && result['data'].key?('name') rescue false end |
permalink #public_url(path, options = {}) ⇒ String
获取文件外网访问地址
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, = {}) 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 |