Method: Baidu::PCS::Client#upload
- Defined in:
- lib/baidu/pcs/client.rb
#upload(file, options = {}) ⇒ Hash
Note:
百度PCS服务目前支持最大2G的单个文件上传
Note:
文件大小超过 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 |