Module: QcloudCos::Api
- Included in:
- QcloudCos
- Defined in:
- lib/qcloud_cos/api.rb
Instance Method Summary collapse
-
#create_folder(path, options = {}) ⇒ Hash
创建目录.
-
#delete(path, options = {}) ⇒ Hash
删除文件或者目录.
-
#delete_file(path, options = {}) ⇒ Hash
删除文件.
-
#delete_folder(path, options = {}) ⇒ Hash
删除目录.
-
#init_slice_upload(path, filesize, sha, options = {}) ⇒ Hash
初始化分片上传.
-
#list(path = '/', options = {}) ⇒ Hash
列出所有文件或者目录.
-
#list_files(path = '/', options = {}) ⇒ Hash
列出所有文件.
-
#list_folders(path = '/', options = {}) ⇒ Hash
列出所有目录.
-
#stat(path, options = {}) ⇒ Hash
查看文件或者文件夹信息.
-
#update(path, biz_attr, options = {}) ⇒ Hash
更新文件或者目录信息.
-
#upload(path, file_or_bin, options = {}) ⇒ Hash
(also: #create)
上传文件.
-
#upload_part(path, session, offset, content, options = {}) ⇒ Hash
上传分片数据.
-
#upload_slice(dst_path, src_path, options = {}, &block) ⇒ Hash
分片上传.
Instance Method Details
#create_folder(path, options = {}) ⇒ Hash
创建目录
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/qcloud_cos/api.rb', line 72 def create_folder(path, = {}) path = fixed_path(path) bucket = validates(path, , :folder_only) url = generate_rest_url(bucket, path) query = { 'op' => 'create' }.merge(Utils.hash_slice(, 'biz_attr')) headers = { 'Authorization' => .sign(bucket), 'Content-Type' => 'application/json' } http.post(url, body: query.to_json, headers: headers).parsed_response end |
#delete(path, options = {}) ⇒ Hash
删除文件或者目录
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/qcloud_cos/api.rb', line 251 def delete(path, = {}) path = fixed_path(path) bucket = validates(path, , 'both') url = generate_rest_url(bucket, path) query = { 'op' => 'delete' } resource = "/#{bucket}#{Utils.url_encode(path)}" headers = { 'Authorization' => .sign_once(bucket, resource), 'Content-Type' => 'application/json' } http.post(url, body: query.to_json, headers: headers).parsed_response end |
#delete_file(path, options = {}) ⇒ Hash
删除文件
306 307 308 309 |
# File 'lib/qcloud_cos/api.rb', line 306 def delete_file(path, = {}) fail InvalidFilePathError if path.end_with?('/') delete(path, ) end |
#delete_folder(path, options = {}) ⇒ Hash
删除目录
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/qcloud_cos/api.rb', line 277 def delete_folder(path, = {}) validates(path, , 'folder_only') return delete(path, ) if ['recursive'] != true loop do objects = list(path, ) objects.each do |object| if object.is_a?(QcloudCos::FolderObject) delete_folder("#{path}#{object.name}/", ) elsif object.is_a?(QcloudCos::FileObject) delete_file("#{path}#{object.name}", ) end end break unless objects.has_more ['context'] = objects.context end delete(path) end |
#init_slice_upload(path, filesize, sha, options = {}) ⇒ Hash
初始化分片上传
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/qcloud_cos/api.rb', line 188 def init_slice_upload(path, filesize, sha, = {}) path = fixed_path(path) bucket = validates(path, ) url = generate_rest_url(bucket, path) query = generate_slice_upload_query(filesize, sha, ) sign = ['sign'] || .sign(bucket) http.post(url, query: query, headers: { 'Authorization' => sign }).parsed_response end |
#list(path = '/', options = {}) ⇒ Hash
列出所有文件或者目录
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/qcloud_cos/api.rb', line 18 def list(path = '/', = {}) path = fixed_path(path) bucket = validates(path, , 'both') query = { 'op' => 'list', 'num' => 100 }.merge(Utils.hash_slice(, 'num', 'pattern', 'order', 'context')) url = generate_rest_url(bucket, path) sign = .sign(bucket) result = http.get(url, query: query, headers: { 'Authorization' => sign }).parsed_response QcloudCos::List.new(result['data']) end |
#list_files(path = '/', options = {}) ⇒ Hash
列出所有文件
44 45 46 47 |
# File 'lib/qcloud_cos/api.rb', line 44 def list_files(path = '/', = {}) Utils.stringify_keys!() list(path, .merge('pattern' => 'eListFileOnly')) end |
#list_folders(path = '/', options = {}) ⇒ Hash
列出所有目录
59 60 61 62 |
# File 'lib/qcloud_cos/api.rb', line 59 def list_folders(path = '/', = {}) Utils.stringify_keys!() list(path, .merge('pattern' => 'eListDirOnly')) end |
#stat(path, options = {}) ⇒ Hash
查看文件或者文件夹信息
318 319 320 321 322 323 324 325 326 327 |
# File 'lib/qcloud_cos/api.rb', line 318 def stat(path, = {}) path = fixed_path(path) bucket = validates(path, , 'both') url = generate_rest_url(bucket, path) query = { 'op' => 'stat' } sign = .sign(bucket) http.get(url, query: query, headers: { 'Authorization' => sign }).parsed_response end |
#update(path, biz_attr, options = {}) ⇒ Hash
更新文件或者目录信息
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/qcloud_cos/api.rb', line 228 def update(path, biz_attr, = {}) path = fixed_path(path) bucket = validates(path, , 'both') url = generate_rest_url(bucket, path) query = { 'op' => 'update', 'biz_attr' => biz_attr } resource = "/#{bucket}#{Utils.url_encode(path)}" headers = { 'Authorization' => .sign_once(bucket, resource), 'Content-Type' => 'application/json' } http.post(url, body: query.to_json, headers: headers).parsed_response end |
#upload(path, file_or_bin, options = {}) ⇒ Hash Also known as: create
上传文件
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/qcloud_cos/api.rb', line 97 def upload(path, file_or_bin, = {}) path = fixed_path(path) bucket = validates(path, ) url = generate_rest_url(bucket, path) query = { 'op' => 'upload' }.merge(Utils.hash_slice(, 'biz_attr')).merge(generate_file_query(file_or_bin)) http.post(url, query: query, headers: { 'Authorization' => .sign(bucket) }).parsed_response end |
#upload_part(path, session, offset, content, options = {}) ⇒ Hash
上传分片数据
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/qcloud_cos/api.rb', line 209 def upload_part(path, session, offset, content, = {}) path = fixed_path(path) bucket = validates(path, ) url = generate_rest_url(bucket, path) query = generate_upload_part_query(session, offset, content) sign = ['sign'] || .sign(bucket) http.post(url, query: query, headers: { 'Authorization' => sign }).parsed_response end |
#upload_slice(dst_path, src_path, options = {}, &block) ⇒ Hash
分片上传
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/qcloud_cos/api.rb', line 133 def upload_slice(dst_path, src_path, = {}, &block) dst_path = fixed_path(dst_path) fail FileNotExistError unless File.exist?(src_path) bucket = validates(dst_path, ) filesize = File.size(src_path) sha = Utils.generate_file_sha(src_path) sign = .sign(bucket) resp = init_slice_upload(dst_path, filesize, sha, .merge('sign' => sign)) data = resp['data'] return data if data.key?('url') # 妙传命中 slice_size = data['slice_size'] || DEFAULT_SLICE_SIZE session = data['session'] || ['session'] offset = data['offset'] || 0 fail MissingSessionIdError unless session while offset < filesize filecontent = IO.read(src_path, slice_size, offset) retry_times = 0 begin result = upload_part(dst_path, session, offset, filecontent, ) progress = [offset + slice_size, filesize].min yield((progress.to_f/filesize).round(2)) if block_given? if result.key?('data') && result['data'].key?('session') session = result['data']['session'] elsif result.key?('data') && result['data'].key?('url') return result['data'] end rescue => e retry_times += 1 retry if retry_times <= config.max_retry_times raise e end offset += slice_size end end |