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
创建目录
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/qcloud_cos/api.rb', line 73 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
删除文件或者目录
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/qcloud_cos/api.rb', line 223 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
删除文件
273 274 275 276 |
# File 'lib/qcloud_cos/api.rb', line 273 def delete_file(path, = {}) fail InvalidFilePathError if path.end_with?('/') delete(path, ) end |
#delete_folder(path, options = {}) ⇒ Hash
删除目录
249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/qcloud_cos/api.rb', line 249 def delete_folder(path, = {}) validates(path, , 'folder_only') return delete(path, ) if ['recursive'] != true all(path, ).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 delete(path) end |
#init_slice_upload(path, filesize, sha, options = {}) ⇒ Hash
初始化分片上传
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/qcloud_cos/api.rb', line 160 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
列出文件或者目录
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/qcloud_cos/api.rb', line 19 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
列出所有文件
45 46 47 48 |
# File 'lib/qcloud_cos/api.rb', line 45 def list_files(path = '/', = {}) Utils.stringify_keys!() list(path, .merge('pattern' => 'eListFileOnly')) end |
#list_folders(path = '/', options = {}) ⇒ Hash
列出所有目录
60 61 62 63 |
# File 'lib/qcloud_cos/api.rb', line 60 def list_folders(path = '/', = {}) Utils.stringify_keys!() list(path, .merge('pattern' => 'eListDirOnly')) end |
#stat(path, options = {}) ⇒ Hash
查看文件或者文件夹信息
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/qcloud_cos/api.rb', line 285 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
更新文件或者目录信息
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/qcloud_cos/api.rb', line 200 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
上传文件
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/qcloud_cos/api.rb', line 98 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
上传分片数据
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/qcloud_cos/api.rb', line 181 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
分片上传
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/qcloud_cos/api.rb', line 134 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, ) multipart = QcloudCos::Multipart.new( dst_path, src_path, .merge(bucket: bucket, authorization: ) ) multipart.upload(&block) multipart.result end |