def test_put_file_relax(self): scs = SCSBucket(self.bucket_name) try: scs.put_bucket('public-read-write') except: self.fail('create bucket:%s is failed'%self.bucket_name) scs.put(self.object_key, 'fileContent...fileContent...fileContent...fileContent...fileContent...') orign_file_metaResult = scs.meta(self.object_key) canned_acl = 'public-read-write' #快捷ACL metadata = {} #自定义文件属性信息 metadata['author'] = 'dage' metadata['home'] = 'tianjin' metadata['age'] = '18' mimetype = 'text/plain' scs.put_relax(key=self.object_key+'_relax', sina_sha1=orign_file_metaResult['Content-SHA1'], s_sina_length=orign_file_metaResult['Size'], acl=canned_acl, metadata=metadata, mimetype=mimetype) relax_file_aclInfo = scs.acl_info(self.object_key+'_relax') self.assertTrue(ACL.ACL_GROUP_ANONYMOUSE in relax_file_aclInfo['ACL'], 'The acl dose not contains GRPS000000ANONYMOUSE group') self.assertTrue(ACL.ACL_READ in relax_file_aclInfo['ACL'][ACL.ACL_GROUP_ANONYMOUSE], 'The acl GRPS000000ANONYMOUSE group hasn\'t read right') self.assertTrue(ACL.ACL_WRITE in relax_file_aclInfo['ACL'][ACL.ACL_GROUP_ANONYMOUSE], 'The acl GRPS000000ANONYMOUSE group hasn\'t write right') relax_file_info = scs.info(self.object_key+'_relax') #assert metadata self.assertEqual(metadata['author'], relax_file_info['metadata']['author'], 'The response metadata[\'author\'] is not match') self.assertEqual(metadata['home'], relax_file_info['metadata']['home'], 'The response metadata[\'home\'] is not match') self.assertEqual(metadata['age'], relax_file_info['metadata']['age'], 'The response metadata[\'age\'] is not match') #assert content-type self.assertEqual(mimetype, relax_file_info['headers']['content-type'], 'The response content-type is not match')
def test_update_file_meta(self): scs = SCSBucket(self.bucket_name) try: scs.put_bucket('public-read-write') except: self.fail('create bucket:%s is failed'%self.bucket_name) content = u'this is a file content text!!' scs.put(self.object_key, content) canned_acl = 'public-read-write' #快捷ACL metadata = {} #自定义文件属性信息 metadata['author'] = 'dage' metadata['home'] = 'tianjin' metadata['age'] = '18' mimetype = 'text/plain' scs.update_meta(self.object_key, metadata=metadata, acl=canned_acl, mimetype=mimetype) file_info = scs.info(self.object_key) #assert metadata self.assertEqual(metadata['author'], file_info['metadata']['author'], 'The response metadata[\'author\'] is not match') self.assertEqual(metadata['home'], file_info['metadata']['home'], 'The response metadata[\'home\'] is not match') self.assertEqual(metadata['age'], file_info['metadata']['age'], 'The response metadata[\'age\'] is not match') #assert content-type # print file_info['headers'] #TODO:服务器问题 self.assertEqual(mimetype, file_info['headers']['content-type'], 'The response content-type is not match') #acl file_aclInfo = scs.acl_info(self.object_key) self.assertTrue(ACL.ACL_GROUP_ANONYMOUSE in file_aclInfo['ACL'], 'The acl dose not contains GRPS000000ANONYMOUSE group') self.assertTrue(ACL.ACL_READ in file_aclInfo['ACL'][ACL.ACL_GROUP_ANONYMOUSE], 'The acl GRPS000000ANONYMOUSE group hasn\'t read right') self.assertTrue(ACL.ACL_WRITE in file_aclInfo['ACL'][ACL.ACL_GROUP_ANONYMOUSE], 'The acl GRPS000000ANONYMOUSE group hasn\'t write right')
def test_copy_file(self): scs = SCSBucket(self.bucket_name) try: scs.put_bucket('public-read-write') except: self.fail('create bucket:%s is failed'%self.bucket_name) scs.putFile(self.object_key, self.local_file_name) canned_acl = 'public-read' #快捷ACL metadata = {} #自定义文件属性信息 metadata['author'] = 'copied' metadata['home'] = 'beijing' metadata['age'] = '189' mimetype = 'text/plain' scs.copy(source='/'+self.bucket_name+'/'+self.object_key, key=self.object_key+'_copied', acl=canned_acl, metadata=metadata, mimetype=mimetype) copied_file_aclInfo = scs.acl_info(self.object_key+'_copied') self.assertTrue(ACL.ACL_GROUP_ANONYMOUSE in copied_file_aclInfo['ACL'], 'The acl dose not contains GRPS000000ANONYMOUSE group') self.assertTrue(ACL.ACL_READ in copied_file_aclInfo['ACL'][ACL.ACL_GROUP_ANONYMOUSE], 'The acl GRPS000000ANONYMOUSE group hasn\'t read right') copied_file_info = scs.info(self.object_key+'_copied') #assert metadata self.assertEqual(metadata['author'], copied_file_info['metadata']['author'], 'The response metadata[\'author\'] is not match') self.assertEqual(metadata['home'], copied_file_info['metadata']['home'], 'The response metadata[\'home\'] is not match') self.assertEqual(metadata['age'], copied_file_info['metadata']['age'], 'The response metadata[\'age\'] is not match') #assert content-type self.assertEqual(mimetype, copied_file_info['headers']['content-type'], 'The response content-type is not match')
def rename(self, com, time_out): old_cloud_name = "haoc/nodes/%s" % com.old_name new_cloud_name = "haoc/nodes/%s" % com.new_name bucket = SCSBucket(BUCKET_NAME, timeout=time_out) try: if new_cloud_name[-1] == '/': bucket.copy(source="/%s/%s" % (BUCKET_NAME, old_cloud_name), key=new_cloud_name) del bucket[old_cloud_name] else: shall_do = True if self.from_agenda: try: bucket["%s.nod" % new_cloud_name] except sinastorage.bucket.KeyNotFound: pass else: info = bucket.info("%s.nod" % new_cloud_name) ct = float(info.get('metadata').get('lmt')) if com.op_time < ct: shall_do = False if shall_do: bucket.copy(source="/%s/%s.nod" % (BUCKET_NAME, old_cloud_name), key="%s.nod" % new_cloud_name) del bucket["%s.nod" % old_cloud_name] bucket.copy(source="/%s/%s.hlp" % (BUCKET_NAME, old_cloud_name), key="%s.hlp" % new_cloud_name) del bucket["%s.hlp" % old_cloud_name] except sinastorage.bucket.KeyNotFound: pass
def fun(_time_out): if self.lis is not None: self.lis = [] bucket = SCSBucket(BUCKET_NAME, timeout=_time_out) items_generator = bucket.listdir(prefix='haoc/nodes/', limit=10000) access_key = HaocUtils.Config.get_ak() for item in items_generator: common_name = item[0][11:] local_name = "%s/data/%s/nodes/%s" % ( HaocUtils.get_root_path(), access_key, common_name) if self.lis is not None: if common_name[-1] == '/' or common_name[-4:] == '.nod': self.lis.append(common_name) if common_name[-1] == '/': if not os.path.exists(local_name): os.makedirs(local_name) elif common_name[-4:]: if os.path.exists(local_name): lt = int(os.path.getmtime("%s.nod" % local_name[:-4])) ct = int( bucket.info( "%s.nod" % item[0][:-4]).get('metadata').get('lmt')) # Compare local file modified time with cloud file modified time if lt < ct: open(local_name, "w").close() if is_manual_sync: print "Updated: %s" % common_name else: if not os.path.exists(os.path.dirname(local_name)): os.makedirs(os.path.dirname(local_name)) open(local_name, "w").close() if is_manual_sync: print "Created: %s" % common_name
def delete(self, com, time_out): cloud_path = "haoc/nodes/%s" % com.path bucket = SCSBucket(BUCKET_NAME, timeout=time_out) if cloud_path[-1] == '/': del bucket[cloud_path] else: shall_do = True if self.from_agenda: try: bucket["%s.nod" % cloud_path] except sinastorage.bucket.KeyNotFound: return else: info = bucket.info("%s.nod" % cloud_path) ct = HaocUtils.utc_to_local_stamp(info['modify']) if com.op_time < ct: shall_do = False if shall_do: del bucket["%s.nod" % cloud_path] del bucket["%s.hlp" % cloud_path]
def create(self, com): if com.path[-1] != '/': self.new_item_start.emit(os.path.basename(com.path)) cloud_path = "haoc/nodes/%s" % com.path bucket = SCSBucket(BUCKET_NAME) local_path = "%s/data/%s/nodes/%s" % ( HaocUtils.get_root_path(), HaocUtils.Config.get_ak(), com.path) if cloud_path[-1] == '/': bucket.put(cloud_path, '') else: shall_upload = True if not os.path.exists("%s.nod" % local_path): print "File not found:%s" % local_path return lt = int(os.path.getmtime("%s.nod" % local_path)) if self.from_agenda: try: bucket["%s.nod" % cloud_path] except sinastorage.bucket.KeyNotFound: pass else: info = bucket.info("%s.nod" % cloud_path) ct = int(info.get('metadata').get('lmt')) if lt < ct: shall_upload = False if shall_upload: bucket.putFile("%s.nod" % cloud_path, "%s.nod" % local_path, self.call_back) bucket.update_meta("%s.nod" % cloud_path, {'lmt': str(lt)}) bucket.update_meta("%s.nod" % cloud_path, {'hver': hou.applicationVersionString()}) if os.path.exists("%s.hlp" % local_path): bucket.putFile("%s.hlp" % cloud_path, "%s.hlp" % local_path) else: open("%s.nod" % local_path, 'w').close()
def test_info_file(self): scs = SCSBucket(self.bucket_name) try: scs.put_bucket('public-read-write') except: self.fail('create bucket:%s is failed'%self.bucket_name) content = u'this is a file content text!!' #文件内容 canned_acl = 'public-read-write' #快捷ACL metadata = {} #自定义文件属性信息 metadata['author'] = 'dage' metadata['home'] = 'tianjin' metadata['age'] = '18' mimetype = 'text/plain' scs.put(self.object_key, content, acl=canned_acl, metadata=metadata, mimetype=mimetype) file_info = scs.info(self.object_key) #assert metadata self.assertEqual(metadata['author'], file_info['metadata']['author'], 'The response metadata[\'author\'] is not match') self.assertEqual(metadata['home'], file_info['metadata']['home'], 'The response metadata[\'home\'] is not match') self.assertEqual(metadata['age'], file_info['metadata']['age'], 'The response metadata[\'age\'] is not match') #assert content-type self.assertEqual(mimetype, file_info['headers']['content-type'], 'The response content-type is not match')
def info_file(): s = SCSBucket('test11') info = s.info('sss.txt') print info['mimetype'] print info['size'] print info