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
删除目录.
-
#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_slice(dst_path, src_path, options = {}) ⇒ 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
删除文件或者目录
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/qcloud_cos/api.rb', line 249 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
删除文件
304 305 306 307 |
# File 'lib/qcloud_cos/api.rb', line 304 def delete_file(path, = {}) fail InvalidFilePathError if path.end_with?('/') delete(path, ) end |
#delete_folder(path, options = {}) ⇒ Hash
删除目录
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/qcloud_cos/api.rb', line 275 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 |
#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
查看文件或者文件夹信息
316 317 318 319 320 321 322 323 324 325 |
# File 'lib/qcloud_cos/api.rb', line 316 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
更新文件或者目录信息
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/qcloud_cos/api.rb', line 226 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_slice(dst_path, src_path, options = {}) ⇒ Hash
分片上传
126 127 128 129 130 131 132 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 |
# File 'lib/qcloud_cos/api.rb', line 126 def upload_slice(dst_path, src_path, = {}) 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)) puts "init slice upload: #{resp}" 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, ) puts "upload part data: #{result}" 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 puts "retry part upload request with offset:#{offset}, slice_size:#{slice_size} ..." retry if retry_times <= config.max_retry_times raise e end offset += slice_size end end |