def GET(self, environ,start_response): req = Request(environ) self.userName = req.headers.get('X-Auth-User', '') self.userKey = req.headers.get('X-Auth-Key', '') self.domain = req.headers.get('domain', '') self.token = '' resheaders = [] root_id = Utils.getRootContainerId(self.domain) if root_id == '': Log().error('can not get the domain ['+self.domain+"] id") start_response(HTTPNotFound,[("Content-type", "text/plain"),]) return '' try: kwargs = {'m_parent_id':root_id} cons_objs_get = DataLogic().get_by_kwargs(**kwargs) if len(cons_objs_get) == 0: start_response(HTTPNoContent,[("Content-type", "text/plain"),]) return '' # #modify by zhangkaixuan # containers = '[' objects = '[' for x in range(len(cons_objs_get)): if cons_objs_get[x].m_content_type == 'container': #containers.append(cons_objs_get[x].m_name) containers = containers + cons_objs_get[x].m_name + ',' elif cons_objs_get[x].m_content_type == 'object': #objects.append(cons_objs_get[x].m_name) objects = objects + cons_objs_get[x].m_name + ',' #cons = ('containers',str(containers)) #objs = ('objects', str(objects)) #count = ('count', len(cons_objs_get)) if containers != '[': containers = containers[:-1] if objects != '[': objects = objects[:-1] all = containers + "]#" + objects +"]" print all param = str(len(all))+'s' bytes = struct.pack(str(param),all) self.content = bytes #resheaders.append(cons) #resheaders.append(objs) #resheaders.append(count) # #modify by zhangkaixuan # start_response(HTTPOk,resheaders) return self.content except Exception,e: Log().error('GET_DOMAIN by '+req.headers.get('X-Auth-User')+': '+req.headers['domain']+' '+str(e)) start_response(HTTPInternalServerError, resheaders) return ''
def DELETE(self, environ,start_response): self.content = '' req = Request(environ) self.userName = req.headers.get('X-Auth-User', '') self.userKey = req.headers.get('X-Auth-Key', '') self.domain = req.headers.get('domain', '') self.object = req.headers.get('object', '') self.size = req.headers.get('content-length', '') containers_object = req.headers.get('container','') containers_object.append(str(self.object)) Log().info("start object GET operation username:"******",domain:"+self.domain+",container:"+str(containers_object)+ ",object:"+self.object) c_len = len(containers_object)# the length of the container root_id = Utils.getRootContainerId(self.domain) parent_id = root_id if parent_id == '': Log().error('can not get the domain ['+self.domain+"] id") start_response(HTTPNotFound,[("Content-type", "text/plain"),]) return '' resheaders = [] for i in xrange(c_len): c_name = containers_object[i].encode("UTF-8") kwargs = {'m_name':c_name, 'm_parent_id':parent_id} containers_object_get = DataLogic().get_by_kwargs(**kwargs) if len(containers_object_get) == 0: if i == c_len-1: Log().error('can not get the object ['+c_name+"] id") start_response(HTTPNotFound,[("Content-type", "text/plain"),]) return '' else: Log().error('can not get the container ['+c_name+"] id") start_response(HTTPNotFound,[("Content-type", "text/plain"),]) return '' break else: if i == c_len-1: self.object = containers_object_get[0].m_storage_name else: self.container = containers_object_get[0].m_storage_name parent_id = containers_object_get[0].id try: Log().info('START DELETE_OBJECT by '+self.userName+': '+self.domain+'/'+self.container+'/'+self.object) token = req.headers['token'] url = req.headers['auth_url'] dic = {"storage_url":url, 'token':token, 'container':self.container,'headers':{}, \ 'object':self.object,} ApiMapping().scloud_delete_object(**dic) kwargs = { 'id': parent_id, } DataLogic().delete_data_by_id(**kwargs) start_response(HTTPOk, [("Content-type", "text/plain"),]) return '' except Exception,e: Log().error('DELETE_OBJECT by '+req.headers.get('X-Auth-User')+': '+req.headers['domain']+'/'+str(req.headers['container'])+' '+str(e)) start_response(HTTPInternalServerError, resheaders) return ''
def POST(self, environ,start_response): '''POST can be used to change object metadata object rename ''' self.content = '' req = Request(environ) self.userName = req.headers.get('X-Auth-User', '') self.userKey = req.headers.get('X-Auth-Key', '') self.domain = req.headers.get('domain', '') self.original_name = req.headers.get('object','').encode("UTF-8") self.current_name = req.headers.get('current-name','').encode("UTF-8") containers = req.headers.get('container','') Log().info("start object POST operation username:"******",domain:"+self.domain+",container:"+str(containers)+",original-name:"+self.original_name+",current-name:"+self.current_name) c_len = len(containers)# the length of the container root_id = Utils.getRootContainerId(self.domain) parent_id = root_id if parent_id == '': Log().error('can not get the domain ['+self.domain+"] id") start_response(HTTPNotFound,[("Content-type", "text/plain"),]) return '' resheaders = [] if self.current_name == '': start_response(HTTPBadRequest,[("Content-type", "text/plain"),]) return 'current_name not assigned\n' for i in xrange(c_len): c_name = containers[i].encode("UTF-8") # get all the containers corresponding to such conditions kwargs = {'m_name':c_name, 'm_parent_id':parent_id, 'm_content_type':'container'} containers_get = DataLogic().get_by_kwargs(**kwargs) if len(containers_get) != 0: parent_id = containers_get[0].id if i == c_len-1: try: kws = {'m_name':self.original_name, 'm_parent_id':parent_id, 'm_content_type':'object'} o_get = DataLogic().get_by_kwargs(**kws) object_id = o_get[0].id kwsExisting = {'m_name':self.current_name, 'm_parent_id':parent_id, 'm_content_type':'object'} o_get_2 = DataLogic().get_by_kwargs(**kwsExisting) if len(o_get_2) == 0: kwargs = {'id':object_id, 'm_name':self.current_name} DataLogic().update_data_by_id(**kwargs) start_response(HTTPOk, resheaders) return self.content else: start_response(HTTPConflict, resheaders) return self.content except Exception,e: Log().error('POST_OBJECT by '+req.headers.get('X-Auth-User')+': '+req.headers['domain']+'/'+str(req.headers['container'])+' '+str(e)) start_response(HTTPInternalServerError, resheaders) return self.content else: Log().error('Containers not Found, POST_OBJECT by '+req.headers.get('X-Auth-User')+': '+req.headers['domain']+'/'+str(req.headers['container'])) start_response(HTTPNotFound, resheaders) return self.content
def PUT(self, environ,start_response): self.content = '' req = Request(environ) self.userName = req.headers.get('X-Auth-User', '') self.userKey = req.headers.get('X-Auth-Key', '') self.domain = req.headers.get('domain', '') containers = req.headers.get('container','') self.object = req.headers.get('object', '').encode("UTF-8") self.size = req.headers.get('content-length', '') c_len = len(containers)# the length of the container root_id = Utils.getRootContainerId(self.domain) parent_id = root_id if parent_id == '': Log().error('can not get the domain ['+self.domain+"] id") start_response(HTTPNotFound,[("Content-type", "text/plain"),]) return '' if c_len == 0: self.container = Utils.getRootContainerStorageName(self.domain) resheaders = [] Log().info("start object PUT operation username:"******",domain:"+self.domain+",containers:"+str(containers)+ ",object:"+self.object) for i in xrange(c_len): c_name = containers[i].encode("UTF-8") kwargs = {'m_name':c_name, 'm_parent_id':parent_id, 'm_content_type':'container'} containers_get = DataLogic().get_by_kwargs(**kwargs) if len(containers_get) == 0: Log().error('Containers not Found, PUT_OBJECT by '+req.headers.get('X-Auth-User')+': '+req.headers['domain']+'/'+str(req.headers['container'])) start_response(HTTPNotFound, resheaders) return self.content else: self.container = containers_get[0].m_storage_name parent_id = containers_get[0].id kwargs = {'m_name':self.object, 'm_parent_id':parent_id, 'm_content_type':'object'} object_get = DataLogic().get_by_kwargs(**kwargs) if len(object_get) > 0: kws = {'id':object_get[0].id} object_get = DataLogic().delete_data_by_id(**kws) try: Log().info('START PUT_OBJECT by '+self.userName+': '+self.domain+'/'+str(containers)+'/'+self.object) token = req.headers['token'] url = req.headers['auth_url'] sfile = req.body_file #transaction control object_name_construct = self.userName+'_'+str(parent_id)+'_'+self.object+'_'+self.domain+'_'+'object_'+"".join(re.split('\W+', str(datetime.datetime.now()))) object_storagename = hashlib.md5(object_name_construct).hexdigest() dic = {"storage_url":url, 'token':token, 'container':self.container,'headers':{}, \ 'object':object_storagename, 'contents':sfile} object_hash = ApiMapping().scloud_put_object(**dic) kwargs = { 'm_name' : self.object, 'm_storage_name' : object_storagename, 'm_domain_name' : self.domain, 'm_content_type' : 'object', 'm_status' : '1', #'1' means available, '0' means not available 'm_uri' : object_storagename, 'm_hash' : object_hash , 'm_size' : '2G', 'm_parent_id' : parent_id, 'created' : str(datetime.datetime.now()), } DataLogic().add_data(**kwargs); for item in kwargs.items(): resheaders.append(item) start_response(HTTPCreated, resheaders) return self.content except Exception,e: Log().error('PUT_OBJECT by '+req.headers.get('X-Auth-User')+': '+req.headers['domain']+'/'+str(req.headers['container'])+' '+str(e)) start_response(HTTPInternalServerError, resheaders) return ''