def put_data( up_token, key, data, params=None, mime_type='application/octet-stream', check_crc=False, progress_handler=None, callback=None): """上传二进制流到七牛 Args: up_token: 上传凭证 key: 上传文件名 data: 上传二进制流 params: 自定义变量,规格参考 http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar mime_type: 上传数据的mimeType check_crc: 是否校验crc32 progress_handler: 上传进度 Returns: 一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"} 一个ReponseInfo对象 """ crc = crc32(data) if check_crc else None _form_put(up_token, key, data, params, mime_type, crc, False, progress_handler,callback=callback)
def upload(self, callback=None): """上传操作""" self.blockStatus = [] host = config.get_default('default_up_host') for block in _file_iter(self.input_stream, config._BLOCK_SIZE): length = len(block) crc = crc32(block) ret, info = yield tornado.gen.Task(self.make_block, block, length, host) if ret is None and not info.need_retry: callback( (ret,info) ) return if info.connect_failed: host = config.UPBACKUP_HOST if info.need_retry or crc != ret['crc32']: ret, info = yield tornado.gen.Task(self.make_block, block, length, host) if ret is None or crc != ret['crc32']: callback( (ret,info) ) return self.blockStatus.append(ret) if(callable(self.progress_handler)): self.progress_handler(((len(self.blockStatus) - 1) * config._BLOCK_SIZE)+length, self.size) self.make_file(host, callback=callback)