Class: Baidu::PCS::Client
- Inherits:
-
Object
- Object
- Baidu::PCS::Client
- Includes:
- Support::Request
- Defined in:
- lib/baidu/pcs/client.rb
Overview
Constant Summary
Constants included from Support::Request
Support::Request::MAX_REDIRECT_LIMIT
1 基本功能 collapse
-
#copy(from, to) ⇒ Hash
单个或批量拷贝文件/目录.
-
#create_super_file(block_list, path, overwrite = false) ⇒ Hash
合并分片文件.
-
#delete(path) ⇒ Hash
单个或批量删除文件/目录.
-
#download(path, options = {}, &block) ⇒ Object
下载单个文件.
-
#list(path, options = {}) ⇒ Hash
获取目录下的文件列表.
-
#meta(path) ⇒ Hash
单个或批量获取文件/目录的元信息.
-
#mkdir(path) ⇒ Hash
创建目录.
-
#move(from, to) ⇒ Hash
单个或批量移动文件/目录.
-
#quota ⇒ Hash
空间配额信息.
-
#search(path, wd, re = false) ⇒ Hash
按文件名搜索文件.
-
#upload(file, options = {}) ⇒ Hash
上传单个文件.
-
#upload_block(data) ⇒ Hash
上传分片文件.
2 高级功能 collapse
-
#add_task(source_url, options = {}) ⇒ Hash
添加离线下载任务,实现单个文件离线下载.
-
#cancel_task(task_id, expires = nil) ⇒ Hash
取消离线下载任务.
-
#diff(cursor = 'null') ⇒ Hash
文件增量更新操作查询.
-
#list_task(options = {}) ⇒ Hash
查询离线下载任务列表.
-
#query_task(task_ids, options = {}) ⇒ Hash
精确查询离线下载任务.
-
#rapid_upload(path, content_length, content_md5, slice_md5, content_crc32, overwrite = false) ⇒ Hash
秒传文件.
-
#stream_list(type, options = {}) ⇒ Hash
获取流式文件列表.
-
#streaming(path, type, &block) ⇒ String
视频转码.
-
#thumbnail(path, width, height, quality = 100, &block) ⇒ Object
获取指定图片文件的缩略图.
3 回收站 collapse
-
#empty ⇒ Hash
清空回收站.
-
#listrecycle(start = 0, limit = 1000) ⇒ Hash
查询回收站文件.
-
#restore(fs_ids) ⇒ Hash
单个或批量还原文件/目录.
Instance Method Summary collapse
-
#initialize(access_token_or_session, dir_name = Baidu.pcs_dir_name) ⇒ Client
constructor
创建一个
Baidu::PCS::Client
文件API 实例,通过此实例可以执行 文件API 调用.
Methods included from Support::Request
Constructor Details
#initialize(access_token, dir_name = Baidu.pcs_dir_name) ⇒ Client #initialize(session, dir_name = Baidu.pcs_dir_name) ⇒ Client
创建一个 Baidu::PCS::Client
文件API 实例,通过此实例可以执行 文件API 调用
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/baidu/pcs/client.rb', line 50 def initialize(access_token_or_session, dir_name=Baidu.pcs_dir_name) @dir_name = dir_name @access_token = case access_token_or_session when String then access_token_or_session when Baidu::Session then access_token_or_session.access_token end raise ArgumentError, 'dir_name must not be blank' if Util.blank? @dir_name raise ArgumentError, 'access_token must not be blank' if Util.blank? @access_token @site = Baidu::PCS::SITE @dir_path = "#{APPS_PATH_PREFIX}/#{@dir_name}" end |
Instance Method Details
#add_task(source_url, options = {}) ⇒ Hash
添加离线下载任务,实现单个文件离线下载
756 757 758 759 760 761 762 763 764 765 766 767 |
# File 'lib/baidu/pcs/client.rb', line 756 def add_task(source_url, ={}) query = { source_url: source_url } query[:timeout] = .delete(:timeout) || 3600 save_path = .delete(:save_path) unless save_path save_path = URI(source_url).path.split('/').last || Time.now.localtime.to_s end save_path = build_path save_path, true query[:save_path] = save_path query.update post "#{BASE_PATH}/services/cloud_dl", query.update(base_query 'add_task') end |
#cancel_task(task_id, expires = nil) ⇒ Hash
取消离线下载任务
893 894 895 896 |
# File 'lib/baidu/pcs/client.rb', line 893 def cancel_task(task_id, expires=nil) query = { task_id: task_id, expires: expires } post "#{BASE_PATH}/services/cloud_dl", query.update(base_query 'cancel_task') end |
#copy(from, to) ⇒ Hash #copy(froms, tos) ⇒ Hash
单个或批量拷贝文件/目录
464 465 466 |
# File 'lib/baidu/pcs/client.rb', line 464 def copy(from, to) move_or_copy :copy, from, to end |
#create_super_file(block_list, path, overwrite = false) ⇒ Hash
合并分片文件
与分片文件上传 #upload_block 方法配合使用,可实现超大文件(>2G)上传,同时也可用于断点续传的场景
208 209 210 211 212 213 214 |
# File 'lib/baidu/pcs/client.rb', line 208 def create_super_file(block_list, path, overwrite=false) raise ArgumentError, 'block_list must be Array' unless block_list.instance_of? Array raise ArgumentError, 'block_list size must be in 2..1024' unless block_list.length.between? 2, 1024 query = build_upload_query 'createsuperfile', path, overwrite param = { block_list: block_list } post "#{BASE_PATH}/file", query, param: JSON.dump(param) end |
#delete(path) ⇒ Hash #delete(paths) ⇒ Hash
文件/目录删除后默认临时存放在回收站内,删除文件或目录的临时存放不占用用户的空间配额;存放有效期为10天,10天内可还原回原路径下,10天后则永久删除
单个或批量删除文件/目录
488 489 490 |
# File 'lib/baidu/pcs/client.rb', line 488 def delete(path) :delete, path end |
#diff(cursor = 'null') ⇒ Hash
本接口有数秒延迟,但保证返回结果为最终一致
文件增量更新操作查询
631 632 633 634 |
# File 'lib/baidu/pcs/client.rb', line 631 def diff(cursor='null') query = { cursor: cursor } get "#{BASE_PATH}/file", query.update(base_query 'diff') end |
#download(path, options = {}) ⇒ String #download(path, options = {}) {|segment| ... } ⇒ void
下载单个文件
Download 接口支持 HTTP 协议标准 range 定义,通过指定 range 的取值可以实现断点下载功能。
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/baidu/pcs/client.rb', line 254 def download(path, ={}, &block) site = Baidu::PCS::DOWNLOAD_SITE query = { path: build_path(path) }.update(base_query 'download') headers = if [:begin] || [:end] range = "#{[:begin] || 0}-#{[:end]}" { Range: "bytes=#{range}" } end if block_given? get "#{BASE_PATH}/file", query, site: site, headers: headers, &block else get "#{BASE_PATH}/file", query, site: site, headers: headers, raw: true end end |
#empty ⇒ Hash
清空回收站
996 997 998 999 |
# File 'lib/baidu/pcs/client.rb', line 996 def empty query = { type: 'recycle' } post "#{BASE_PATH}/file", query.update(base_query 'delete') end |
#list(path, options = {}) ⇒ Hash
获取目录下的文件列表
382 383 384 385 386 387 388 |
# File 'lib/baidu/pcs/client.rb', line 382 def list(path, ={}) query = { path: build_path(path) } query[:by] = [:by] || 'name' query[:order] = [:order] || 'desc' query[:limit] = [:limit] get "#{BASE_PATH}/file", query.update(base_query 'list') end |
#list_task(options = {}) ⇒ Hash
查询离线下载任务列表
查询离线下载任务ID列表及任务信息
879 880 881 882 |
# File 'lib/baidu/pcs/client.rb', line 879 def list_task(={}) query = .dup post "#{BASE_PATH}/services/cloud_dl", query.update(base_query 'list_task') end |
#listrecycle(start = 0, limit = 1000) ⇒ Hash
查询回收站文件
获取回收站中的文件及目录列表
943 944 945 946 |
# File 'lib/baidu/pcs/client.rb', line 943 def listrecycle(start=0, limit=1000) query = { start:start, limit: limit } get "#{BASE_PATH}/file", query.update(base_query 'listrecycle') end |
#meta(path) ⇒ Hash #meta(paths) ⇒ Hash
单个或批量获取文件/目录的元信息
336 337 338 |
# File 'lib/baidu/pcs/client.rb', line 336 def (path) :meta, path end |
#mkdir(path) ⇒ Hash
创建目录
为当前用户创建一个目录
289 290 291 292 |
# File 'lib/baidu/pcs/client.rb', line 289 def mkdir(path) query = { path: build_path(path, true) } post "#{BASE_PATH}/file", query.update(base_query 'mkdir') end |
#move(from, to) ⇒ Hash #move(froms, tos) ⇒ Hash
单个或批量移动文件/目录
425 426 427 |
# File 'lib/baidu/pcs/client.rb', line 425 def move(from, to) move_or_copy :move, from, to end |
#query_task(task_id, options = {}) ⇒ Hash #query_task(task_ids, options = {}) ⇒ Hash
精确查询离线下载任务
根据任务ID号,查询离线下载任务信息及进度信息
836 837 838 839 840 |
# File 'lib/baidu/pcs/client.rb', line 836 def query_task(task_ids, ={}) task_ids = task_ids.join(',') if task_ids.is_a? Array query = { task_ids: task_ids }.update post "#{BASE_PATH}/services/cloud_dl", query.update(base_query 'query_task') end |
#quota ⇒ Hash
空间配额信息
获取当前用户空间配额信息
76 77 78 |
# File 'lib/baidu/pcs/client.rb', line 76 def quota get "#{BASE_PATH}/quota", base_query('info') end |
#rapid_upload(path, content_length, content_md5, slice_md5, content_crc32, overwrite = false) ⇒ Hash
被秒传文件必须大于256KB(即 256*1024 B);校验段为文件的前256KB,秒传接口需要提供校验段的MD5。
非强一致接口,上传后请等待1秒后再读取
秒传文件
729 730 731 732 733 734 735 736 737 738 |
# File 'lib/baidu/pcs/client.rb', line 729 def rapid_upload(path, content_length, content_md5, slice_md5, content_crc32, overwrite=false) path = build_path path, true query = { :path => path, :'content-length' => content_length, :'content-md5' => content_md5, :'slice-md5' => slice_md5, :'content-crc32' => content_crc32 } query[:ondup] = overwrite ? 'overwrite' : 'newcopy' post "#{BASE_PATH}/file", query.update(base_query 'rapidupload') end |
#restore(fs_id) ⇒ Hash #restore(fs_ids) ⇒ Hash
非强一致接口,调用后请sleep 1秒读取
单个或批量还原文件/目录
976 977 978 979 980 981 982 983 984 985 986 987 |
# File 'lib/baidu/pcs/client.rb', line 976 def restore(fs_ids) query = case fs_ids when String { fs_id: fs_ids } when Array fs_ids = fs_ids.map { |id| { fs_id: id } } { param: JSON.dump({ list: fs_ids }) } else raise ArgumentError, 'fs_id(s) must be kind of String or Array' end post "#{BASE_PATH}/file", query.update(base_query 'restore') end |
#search(path, wd, re = false) ⇒ Hash
不支持查找目录
按文件名搜索文件
526 527 528 529 530 |
# File 'lib/baidu/pcs/client.rb', line 526 def search(path, wd, re=false) path = build_path path query = { path: path, wd: wd, re: (re ? 1 : 0) } get "#{BASE_PATH}/file", query.update(base_query 'search') end |
#stream_list(type, options = {}) ⇒ Hash
获取流式文件列表
以视频、音频、图片及文档四种类型的视图获取所创建应用程序下的文件列表。
693 694 695 696 |
# File 'lib/baidu/pcs/client.rb', line 693 def stream_list(type, ={}) query = { type: type }.update get "#{BASE_PATH}/stream", query.update(base_query 'list') end |
#streaming(path, type, &block) ⇒ String
目前这个接口支持的源文件格式如下:m3u8/m3u/asf/avi/flv/gif/mkv/mov/mp4/m4a/3gp/3g2/mj2/mpeg/ts/rm/rmvb/webm
视频转码
对视频文件进行转码,实现实时观看视频功能。可下载支持HLS/M3U8的媒体云播放器SDK配合使用。
644 645 646 647 648 649 650 651 652 |
# File 'lib/baidu/pcs/client.rb', line 644 def streaming(path, type, &block) path = build_path path query = { path: path, type: type }.update base_query('streaming') if block_given? get "#{BASE_PATH}/file", query, &block else get "#{BASE_PATH}/file", query, raw: true end end |
#thumbnail(path, width, height, quality = 100) ⇒ String #thumbnail(path, width, height, quality = 100) {|segment| ... } ⇒ void
限制条件:原图大小(0, 10M];原图类型: jpg、jpeg、bmp、gif、png;目标图类型和原图的类型有关;例如:原图是gif图片,则缩略后也为gif图片
获取指定图片文件的缩略图
565 566 567 568 569 570 571 572 573 |
# File 'lib/baidu/pcs/client.rb', line 565 def thumbnail(path, width, height, quality=100, &block) path = build_path path query = { path: path, width: width, height: height, quality: quality }.update base_query('generate') if block_given? get "#{BASE_PATH}/thumbnail", query, &block else get "#{BASE_PATH}/thumbnail", query, raw: true end end |
#upload(file, options = {}) ⇒ Hash
百度PCS服务目前支持最大2G的单个文件上传
文件大小超过 128MB 时,自动启用文件分块上传;如果不想启用文件分块上传,可以通过 block_upload: false 来关闭
上传单个文件
123 124 125 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 |
# File 'lib/baidu/pcs/client.rb', line 123 def upload(file, ={}) raise ArgumentError, 'file must be an instance of File' unless file.instance_of? File path = [:path] || File.basename(file) size = file.size if ([:block_upload] && size >= 4*1024*1024*2) || # at least 2 blocks ([:block_upload].nil? && size >= 128*1024*1024) block_size = 4*1024*1024 while block_size * 1024 < size # at most 1024 blocks1 block_size *= 2 end offset, block_list = 0, [] max_retry_times = [:retry_times] || 5 retry_waitsec = [:retry_waitsec] || 30 loop do with_retries(max_retry_times, retry_waitsec) do rest = upload_block IO.binread(file, block_size, offset) block_list << rest[:md5] end offset += block_size break if offset >= size end with_retries(max_retry_times, retry_waitsec) do create_super_file block_list, path, [:overwrite] end else raise IOError, 'file is too large (larger than 2G)' if size > 2*1024*1024*1024 query = build_upload_query 'upload', path, [:overwrite] post "#{BASE_PATH}/file", query, { file: file }, site: Baidu::PCS::UPLOAD_SITE end end |
#upload_block(data) ⇒ Hash
百度PCS服务支持每次直接上传最大2G的单个文件。如需支持上传超大文件(>2G),则可以通过组合调用分片文件上传方法和合并分片文件方法实现:
首先,将超大文件分割为2G以内的单文件,并调用 {#upload_block} 将分片文件依次上传;
其次,调用 {#create_super_file} ,完成分片文件的重组。
除此之外,如果应用中需要支持断点续传的功能,也可以通过分片上传文件并调用 #create_super_file 的方式实现。
上传分片文件
172 173 174 175 |
# File 'lib/baidu/pcs/client.rb', line 172 def upload_block(data) query = build_upload_query 'upload', nil, nil, true post "#{BASE_PATH}/file", query, { file: StringIO.new(data) }, site: Baidu::PCS::UPLOAD_SITE end |