def test_list_bucket_with_tagging(self): from oss2.models import Tagging, TaggingRule auth = oss2.Auth(OSS_ID, OSS_SECRET) service = oss2.Service(auth, OSS_ENDPOINT) bucket_name1 = random_string(63).lower() bucket1 = oss2.Bucket(auth, OSS_ENDPOINT, bucket_name1) bucket1.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() bucket_name2 = random_string(63).lower() bucket2 = oss2.Bucket(auth, OSS_ENDPOINT, bucket_name2) bucket2.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() rule = TaggingRule() rule.add('tagging_key_test_test1', 'value1') rule.add('tagging_key_test1', 'value1') tagging1 = Tagging(rule) try: result = bucket1.put_bucket_tagging(tagging1) except oss2.exceptions.OssError: self.assertFalse(True, 'should not get exception') pass rule = TaggingRule() rule.add('tagging_key2', 'value2') tagging2 = Tagging(rule) try: result = bucket2.put_bucket_tagging(tagging2) except oss2.exceptions.OssError: self.assertFalse(True, 'should not get exception') pass params = {} params['tag-key'] = 'tagging_key_test_test1' params['tag-value'] = 'value1' result = service.list_buckets(params=params) self.assertEqual(1, len(result.buckets)) result = service.list_buckets() self.assertTrue(len(result.buckets) > 1) bucket1.delete_bucket() bucket2.delete_bucket()
def test_lifecycle_object_tagging_exceptions_too_much_rules(self): from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle, StorageTransition, Tagging, TaggingRule rule = LifecycleRule( random_string(10), '中文前缀/', status=LifecycleRule.ENABLED, expiration=LifecycleExpiration( created_before_date=datetime.date(2016, 12, 25))) rule.storage_transitions = [ StorageTransition(created_before_date=datetime.date(2016, 12, 20), storage_class=oss2.BUCKET_STORAGE_CLASS_IA) ] tagging = Tagging() for i in range(1, 20): key = 'test_key_' + str(i) value = 'test_value_' + str(i) tagging.tag_set.tagging_rule[key] = value rule.tagging = tagging lifecycle = BucketLifecycle([rule]) try: # do not return error,but the lifecycle rule doesn't take effect result = self.bucket.put_bucket_lifecycle(lifecycle) except oss2.exceptions.OssError: self.assertFalse( True, "put lifecycle with tagging should fail ,but success")
def test_bucket_tagging(self): from oss2.models import Tagging, TaggingRule rule = TaggingRule() self.assertRaises(oss2.exceptions.ClientError, rule.add, 129 * 'a', 'test') self.assertRaises(oss2.exceptions.ClientError, rule.add, 'test', 257 * 'a') self.assertRaises(oss2.exceptions.ClientError, rule.add, None, 'test') self.assertRaises(oss2.exceptions.ClientError, rule.add, '', 'test') self.assertRaises(KeyError, rule.delete, 'not_exist') tagging = Tagging() tagging.tag_set.tagging_rule['%@abc'] = 'abc' tagging.tag_set.tagging_rule['123++'] = '++123%' try: result = self.bucket.put_bucket_tagging(tagging) except oss2.exceptions.OssError: self.assertFalse(True, 'should not get exception') pass result = self.bucket.get_bucket_tagging() tag_rule = result.tag_set.tagging_rule self.assertEqual(2, len(tag_rule)) self.assertEqual('abc', tag_rule['%@abc']) self.assertEqual('++123%', tag_rule['123++']) result = self.bucket.delete_bucket_tagging() self.assertEqual(int(result.status) / 100, 2)
def test_lifecycle_object_tagging(self): from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle, StorageTransition, Tagging, TaggingRule rule = LifecycleRule(random_string(10), 'aaaaaaaaaaa/', status=LifecycleRule.ENABLED, expiration=LifecycleExpiration(created_before_date=datetime.date(2016, 12, 25))) rule.storage_transitions = [StorageTransition(created_before_date=datetime.date(2016, 12, 20), storage_class=oss2.BUCKET_STORAGE_CLASS_IA)] tagging_rule = TaggingRule() tagging_rule.add('test_key', 'test_value') tagging = Tagging(tagging_rule) rule.tagging = tagging lifecycle = BucketLifecycle([rule]) self.bucket.put_bucket_lifecycle(lifecycle) wait_meta_sync() result = self.bucket.get_bucket_lifecycle() self.assertEqual(1, len(result.rules)) self.assertEqual(1, len(result.rules[0].storage_transitions)) self.assertEqual(datetime.date(2016, 12, 20), result.rules[0].storage_transitions[0].created_before_date) self.assertEqual(1, result.rules[0].tagging.tag_set.len()) self.assertEqual('test_value', result.rules[0].tagging.tag_set.tagging_rule['test_key']) self.bucket.delete_bucket_lifecycle()
def test_lifecycle_without_versioning(self): auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-lifecycle-without-versioning" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() rule1 = LifecycleRule('rule1', 'tests/', status=LifecycleRule.ENABLED, expiration=LifecycleExpiration(days=3)) rule2 = LifecycleRule( 'rule2', 'logging-', status=LifecycleRule.ENABLED, expiration=LifecycleExpiration( created_before_date=datetime.date(2020, 12, 12))) rule3 = LifecycleRule( 'rule3', 'tests1/', status=LifecycleRule.ENABLED, abort_multipart_upload=AbortMultipartUpload(days=3)) rule4 = LifecycleRule( 'rule4', 'logging1-', status=LifecycleRule.ENABLED, abort_multipart_upload=AbortMultipartUpload( created_before_date=datetime.date(2020, 12, 12))) tagging_rule = TaggingRule() tagging_rule.add('test_key1', 'test_value1') tagging_rule.add('test_key2', 'test_value2') tagging = Tagging(tagging_rule) rule5 = LifecycleRule( 'rule5', 'logging2-', status=LifecycleRule.ENABLED, storage_transitions=[ StorageTransition(days=100, storage_class=oss2.BUCKET_STORAGE_CLASS_IA), StorageTransition( days=356, storage_class=oss2.BUCKET_STORAGE_CLASS_ARCHIVE) ], tagging=tagging) lifecycle = BucketLifecycle([rule1, rule2, rule3, rule4, rule5]) bucket.put_bucket_lifecycle(lifecycle) lifecycle = bucket.get_bucket_lifecycle() self.assertEquals(5, len(lifecycle.rules)) bucket.delete_bucket()
def test_object_tagging(self): key = 'requestpayment-test-put-get-delete-object-tagging' content = 'test-content' self.bucket.put_object(key, content) rule = TaggingRule() rule.add('key1', 'value1') rule.add('key2', 'value2') tagging = Tagging(rule) # Put object tagging without payer setting, should be failed. self.assertRaises(oss2.exceptions.AccessDenied, self.payer_bucket.put_object_tagging, key, tagging) # Put object tagging with payer setting, should be successful. headers = dict() headers[OSS_REQUEST_PAYER] = "requester" result = self.payer_bucket.put_object_tagging(key, tagging, headers=headers) self.assertEqual(result.status, 200) # Get object tagging without payer setting, should be failed. self.assertRaises(oss2.exceptions.AccessDenied, self.payer_bucket.get_object_tagging, key) # Get object tagging with payer setting, should be successful. result = self.payer_bucket.get_object_tagging(key, headers=headers) self.assertEqual(len(result.tag_set.tagging_rule), 2) # Delete object tagging without payer setting, should be failed. self.assertRaises(oss2.exceptions.AccessDenied, self.payer_bucket.delete_object_tagging, key) # Delete object tagging with payer setting, should be successful. result = self.payer_bucket.delete_object_tagging(key, headers=headers) self.assertEqual(result.status, 204) self.bucket.delete_object(key)
def test_put_object_tagging_with_versioning(self): from oss2.models import BucketVersioningConfig from oss2.models import Tagging auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-put-object-tagging-version" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() result = bucket.get_bucket_info() self.assertEqual(int(result.status) / 100, 2) self.assertEqual(result.bucket_encryption_rule.sse_algorithm, None) self.assertEqual(result.versioning_status, "Enabled") result = bucket.put_object("test", "test1") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid1 = result.versionid result = bucket.put_object("test", "test2") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid2 = result.versionid self.assertTrue(versionid1 != versionid2) tagging = Tagging() tagging.tag_set.add('k1', 'v1') tagging.tag_set.add('+++', ':::') # put object tagging without version result = bucket.put_object_tagging("test", tagging) self.assertEqual(int(result.status) / 100, 2) params = dict() params['versionId'] = versionid2 result = bucket.get_object_tagging("test", params=params) self.assertEqual(int(result.status) / 100, 2) rule = result.tag_set.tagging_rule self.assertEqual('v1', rule['k1']) self.assertEqual(':::', rule['+++']) tagging = Tagging() tagging.tag_set.add('k2', 'v2') tagging.tag_set.add(':::', '+++') params['versionId'] = versionid1 # put object tagging with version result = bucket.put_object_tagging("test", tagging, params=params) self.assertEqual(int(result.status) / 100, 2) result = bucket.get_object_tagging("test", params=params) self.assertEqual(int(result.status) / 100, 2) rule = result.tag_set.tagging_rule self.assertEqual('v2', rule['k2']) self.assertEqual('+++', rule[':::']) result = bucket.delete_object_tagging("test", params=params) self.assertEqual(int(result.status), 204) params['versionId'] = versionid2 result = bucket.delete_object_tagging("test", params=params) self.assertEqual(int(result.status), 204) result = bucket.delete_object("test") self.assertEqual(int(result.status), 204) delete_marker_versionid = result.versionid self.assertTrue(delete_marker_versionid is not None) params['versionId'] = versionid2 try: result = bucket.get_object("test", params=params) self.assertFalse(True) except: pass # delete 'DELETE' mark bucket.delete_object("test", params={'versionId': delete_marker_versionid}) bucket.delete_object("test", params={'versionId': versionid1}) bucket.delete_object("test", params={'versionId': versionid2}) bucket.delete_bucket()