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_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_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)
示例#5
0
    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)