def scan(self, batch_size=None, scan_key=None): params = self.dump() if 'skip' in params: raise LeanCloudError(1, 'Query.scan dose not support skip option') if 'limit' in params: raise LeanCloudError(1, 'Query.scan dose not support limit option') return Cursor(self._query_class, batch_size, scan_key, params)
def _save_to_cos(self): hex_octet = lambda: hex(int(0x10000 * (1 + random.random())))[-4:] key = ''.join(hex_octet() for _ in range(4)) key = '{0}.{1}'.format(key, self.extension) data = { 'name': self._name, 'key': key, 'ACL': self._acl, 'mime_type': self._type, 'metaData': self._metadata, } response = client.post('/fileTokens', data) content = response.json() self.id = content['objectId'] self._url = content['url'] uptoken = content['token'] headers = { 'Authorization': uptoken, } self._source.seek(0) data = { 'op': 'upload', 'filecontent': self._source.read(), } response = requests.post(content['upload_url'], headers=headers, files=data) self._source.seek(0) info = response.json() if info['code'] != 0: raise LeanCloudError( 1, 'this file is not saved, qcloud cos status code: {}'.format( info['code']))
def _save_to_qiniu(self): import qiniu output = BytesIO() self._source.seek(0) base64.encode(self._source, output) self._source.seek(0) output.seek(0) hex_octet = lambda: hex(int(0x10000 * (1 + random.random())))[-4:] key = ''.join(hex_octet() for _ in range_type(4)) key = '{0}.{1}'.format(key, self.extension) data = { 'name': self._name, 'key': key, 'ACL': self._acl, 'mime_type': self._type, 'metaData': self._metadata, } response = client.post('/qiniu', data) content = response.json() self.id = content['objectId'] self._url = content['url'] uptoken = content['token'] ret, info = qiniu.put_data(uptoken, key, self._source) if info.status_code != 200: raise LeanCloudError( 1, 'the file is not saved, qiniu status code: {0}'.format( info.status_code))
def _save_to_s3(self, token, upload_url): self._source.seek(0) response = requests.put(upload_url, data=self._source, headers={'Content-Type': self.mime_type}) if response.status_code != 200: self._save_callback(token, False) raise LeanCloudError(1, 'The file is not successfully saved to S3') self._source.seek(0) self._save_callback(token, True)
def _save_to_qiniu(self, token, key): import qiniu self._source.seek(0) ret, info = qiniu.put_data(token, key, self._source) self._source.seek(0) if info.status_code != 200: self._save_callback(token, False) raise LeanCloudError( 1, 'the file is not saved, qiniu status code: {0}'.format( info.status_code)) self._save_callback(token, True)
def get(self, object_id): """ 根据 objectId 查询。 :param object_id: 要查询对象的 objectId :return: 查询结果 :rtype: Object """ if not object_id: raise LeanCloudError(code=101, error='Object not found.') obj = self._query_class.create_without_data(object_id) obj.fetch(select=self._select, include=self._include) return obj
def save(self): if self._source: output = cStringIO.StringIO() self._source.seek(0) base64.encode(self._source, output) self._source.seek(0) output.seek(0) hex_octet = lambda: hex(int(0x10000 * (1 + random.random())))[-4:] key = ''.join(hex_octet() for _ in xrange(4)) key = '{0}.{1}'.format(key, self.extension) data = { 'name': self._name, 'key': key, 'ACL': self._acl, 'mime_type': self._type, 'metaData': self._metadata, } response = client.post('/qiniu', data) content = utils.response_to_json(response) self.id = content['objectId'] self._url = content['url'] uptoken = content['token'] ret, info = qiniu.put_data(uptoken, key, self._source) if info.status_code != 200: raise LeanCloudError( 1, 'the file is not saved, qiniu status code: {0}'.format( info.status_code)) elif self._url and self.metadata.get('__source') == 'external': data = { 'name': self._name, 'ACL': self._acl, 'metaData': self._metadata, 'mime_type': self._type, 'url': self._url, } response = client.post('/files/{0}'.format(self._name), data) content = utils.response_to_json(response) self._name = content['name'] self._url = content['url'] self.id = content['objectId'] if 'size' in content: self._metadata['size'] = content['size'] else: raise ValueError return self
def first(self): """ 根据查询获取最多一个对象。 :return: 查询结果 :rtype: Object :raise: LeanCloudError """ params = self.dump() params['limit'] = 1 content = self._do_request(params) results = content['results'] if not results: raise LeanCloudError(101, 'Object not found') obj = self._new_object() obj._update_data(self._process_result(results[0])) return obj
def _save_to_qcloud(self, token, upload_url): headers = { 'Authorization': token, } self._source.seek(0) data = { 'op': 'upload', 'filecontent': self._source.read(), } response = requests.post(upload_url, headers=headers, files=data) self._source.seek(0) info = response.json() if info['code'] != 0: self._save_callback(token, False) raise LeanCloudError( 1, 'this file is not saved, qcloud cos status code: {}'.format( info['code'])) self._save_callback(token, True)
def first(self): """ 根据查询获取最多一个对象。 :return: 查询结果 :rtype: Object :raise: LeanCloudError """ params = self.dump() params['limit'] = 1 content = utils.response_to_json( client.get('/classes/{0}'.format(self._query_class._class_name), params)) results = content['results'] if not results: raise LeanCloudError(101, 'Object not found') obj = self._new_object() obj._finish_fetch(self._process_result(results[0]), True) return obj
def first(self): """ 根据查询获取最多一个对象。 :return: 查询结果 :rtype: Object :raise: LeanCloudError """ params = self.dump() params['limit'] = 1 content = client.get( '/classes/{0}'.format(self._query_class._class_name), params).json() results = content['results'] if not results: raise LeanCloudError(101, 'Object not found') obj = self._new_object() obj._update_data(self._process_result(results[0])) return obj
def _save_to_qiniu(self, token, key): self._source.seek(0) import qiniu qiniu.set_default(connection_timeout=self.timeout) if six.PY3: # use patched put_data implementation for py3k ret, info = self._save_to_qiniu_internal_py3(token, key) else: # use put_data implementation provided by qiniu-sdk ret, info = qiniu.put_data(token, key, self._source) self._source.seek(0) if info.status_code != 200: self._save_callback(token, False) raise LeanCloudError( 1, 'the file is not saved, qiniu status code: {0}'.format( info.status_code)) self._save_callback(token, True)
def _save_to_qcloud(self, token, upload_url): headers = { "Authorization": token, } self._source.seek(0) data = { "op": "upload", "filecontent": self._source.read(), } response = requests.post(upload_url, headers=headers, files=data) self._source.seek(0) info = response.json() if info["code"] != 0: self._save_callback(token, False) raise LeanCloudError( 1, "this file is not saved, qcloud cos status code: {}".format( info["code"]), ) self._save_callback(token, True)
def destroy(self): if not self.id: return False response = client.delete('/files/{0}'.format(self.id)) if response.status_code != 200: raise LeanCloudError(1, "the file is not sucessfully destroyed")
def save(self, *args, **kwargs): raise LeanCloudError(code=1, error='Notification does not support modify')