Class: COS::API
- Inherits:
-
Object
- Object
- COS::API
- Defined in:
- lib/cos/api.rb
Overview
腾讯云对象存储服务RestfulAPI
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
Instance Method Summary collapse
-
#create_folder(path, options = {}) ⇒ Object
创建目录.
-
#delete(path, options = {}) ⇒ Object
删除文件及目录.
-
#download(access_url, file_store, options = {}) ⇒ Object
下载文件.
-
#initialize(config) ⇒ API
constructor
初始化.
-
#list(path, options = {}) ⇒ Object
目录列表/前缀搜索.
-
#stat(path, options = {}) ⇒ Object
目录/文件信息查询.
-
#update(path, biz_attr, options = {}) ⇒ Object
更新目录/文件信息(biz_attr).
-
#upload(path, file_name, file_src, options = {}) ⇒ Object
上传文件(完整上传).
-
#upload_slice(path, file_name, file_src, options = {}) {|Float| ... } ⇒ Object
上传文件(分片上传).
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/cos/api.rb', line 12 def config @config end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
12 13 14 |
# File 'lib/cos/api.rb', line 12 def http @http end |
Instance Method Details
#create_folder(path, options = {}) ⇒ Object
创建目录
41 42 43 44 45 46 47 48 49 |
# File 'lib/cos/api.rb', line 41 def create_folder(path, = {}) bucket = config.get_bucket([:bucket]) sign = http.signature.multiple(bucket) payload = {op: 'create', biz_attr: [:biz_attr]} resource_path = Util.get_resource_path(config.app_id, bucket, path) http.post(resource_path, {}, sign, payload.to_json) .merge({name: resource_path.split('/').at(-1)}) end |
#delete(path, options = {}) ⇒ Object
删除文件及目录
248 249 250 251 252 253 254 255 |
# File 'lib/cos/api.rb', line 248 def delete(path, = {}) bucket = config.get_bucket([:bucket]) resource_path = Util.get_resource_path_or_file(config.app_id, bucket, path) sign = http.signature.once(bucket, path) payload = {op: 'delete'} http.post(resource_path, {}, sign, payload.to_json) end |
#download(access_url, file_store, options = {}) ⇒ Object
Note:
SDK会自动对私有读的Bucket进行签名
下载文件
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/cos/api.rb', line 268 def download(access_url, file_store, = {}) bucket = config.get_bucket([:bucket]) sign = http.signature.multiple(bucket) response = RestClient::Request.execute( :method => 'GET', :url => "#{access_url}?sign=#{sign}", :headers => [:headers] ) if response.code < 300 File.open(file_store, 'wb') do |w| w.write(response.body) end else raise DownloadError, "server response http code: #{response.code}" end end |
#list(path, options = {}) ⇒ Object
目录列表/前缀搜索
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/cos/api.rb', line 167 def list(path, = {}) bucket = config.get_bucket([:bucket]) sign = http.signature.multiple(bucket) resource_path = Util.get_resource_path(config.app_id, bucket, path, [:prefix]) pattern = case [:pattern].to_s.to_sym when :dir_only 'eListDirOnly' when :file_only 'eListFileOnly' else 'eListBoth' end query = { op: 'list', num: [:num] || 20, pattern: pattern, order: [:order].to_s.to_sym == :desc ? 1 : 0, context: [:context] } http.get(resource_path, {params: query}, sign) end |
#stat(path, options = {}) ⇒ Object
目录/文件信息查询
231 232 233 234 235 236 237 |
# File 'lib/cos/api.rb', line 231 def stat(path, = {}) bucket = config.get_bucket([:bucket]) sign = http.signature.multiple(bucket) resource_path = Util.get_resource_path_or_file(config.app_id, bucket, path) http.get(resource_path, {params: {op: 'stat'}}, sign) end |
#update(path, biz_attr, options = {}) ⇒ Object
更新目录/文件信息(biz_attr)
203 204 205 206 207 208 209 210 |
# File 'lib/cos/api.rb', line 203 def update(path, biz_attr, = {}) bucket = config.get_bucket([:bucket]) resource_path = Util.get_resource_path_or_file(config.app_id, bucket, path) sign = http.signature.once(bucket, path) payload = {op: 'update', biz_attr: biz_attr} http.post(resource_path, {}, sign, payload.to_json) end |
#upload(path, file_name, file_src, options = {}) ⇒ Object
上传文件(完整上传)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cos/api.rb', line 68 def upload(path, file_name, file_src, = {}) bucket = config.get_bucket([:bucket]) sign = http.signature.multiple(bucket) resource_path = Util.get_resource_path(config.app_id, bucket, path, file_name) payload = { op: 'upload', sha: Util.file_sha1(file_src), filecontent: File.new(file_src, 'rb'), biz_attr: [:biz_attr] } http.post(resource_path, {}, sign, payload) end |
#upload_slice(path, file_name, file_src, options = {}) {|Float| ... } ⇒ Object
上传文件(分片上传)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/cos/api.rb', line 115 def upload_slice(path, file_name, file_src, = {}, &block) slice = Slice.new( config: config, http: http, path: path, file_name: file_name, file_src: file_src, options: , progress: block ).upload { access_url: slice[:access_url], url: slice[:url], resource_path: slice[:resource_path] } end |