Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_lifecycle_all_without_object_expiration(self):
from oss2.models import LifecycleRule, BucketLifecycle, AbortMultipartUpload, StorageTransition
rule = LifecycleRule(random_string(10), '中文前缀/',
status=LifecycleRule.ENABLED)
rule.abort_multipart_upload = AbortMultipartUpload(days=356)
rule.storage_transitions = [StorageTransition(days=356, storage_class=oss2.BUCKET_STORAGE_CLASS_IA)]
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(356, result.rules[0].abort_multipart_upload.days)
self.assertEqual(1, len(result.rules[0].storage_transitions))
self.assertEqual(356, result.rules[0].storage_transitions[0].days)
self.bucket.delete_bucket_lifecycle()
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(days=357))
self.assertRaises(oss2.exceptions.ClientError,
LifecycleExpiration, days=356, created_before_date=datetime.date(2016, 12, 25))
self.assertRaises(oss2.exceptions.ClientError,
AbortMultipartUpload, days=356, created_before_date=datetime.date(2016, 12, 25))
self.assertRaises(oss2.exceptions.ClientError,
StorageTransition, days=356, created_before_date=datetime.date(2016, 12, 25))
rule.abort_multipart_upload = AbortMultipartUpload(days=356)
rule.storage_transitions = [StorageTransition(days=356, storage_class=oss2.BUCKET_STORAGE_CLASS_IA)]
lifecycle = BucketLifecycle([rule])
self.bucket.put_bucket_lifecycle(lifecycle)
result = self.bucket.get_bucket_lifecycle()
self.assertEqual(1, len(result.rules))
self.assertEqual(356, result.rules[0].abort_multipart_upload.days)
self.assertEqual(1, len(result.rules[0].storage_transitions))
self.assertEqual(356, result.rules[0].storage_transitions[0].days)
self.bucket.delete_bucket_lifecycle()
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()
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()
tagging.tag_set.tagging_rule[129*'a'] = 'test'
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")
del tagging.tag_set.tagging_rule[129*'a']
tagging.tag_set.tagging_rule['%&'] = 'test'
lifecycle.rules[0].tagging = tagging
try:
# do not return error,but the lifecycle rule doesn't take effect
result = self.bucket.put_bucket_lifecycle(lifecycle)
self.assertFalse(True, "put lifecycle with tagging should fail ,but success")
except oss2.exceptions.OssError:
from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle
rule = LifecycleRule(random_string(10), '中文前缀/',
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(days=6))
# 转储为ARCHIVE的天数小于转储为IA
rule.storage_transitions = [oss2.models.StorageTransition(days=5,
storage_class=oss2.BUCKET_STORAGE_CLASS_IA),
oss2.models.StorageTransition(days=2,
storage_class=oss2.BUCKET_STORAGE_CLASS_ARCHIVE)]
self.assertRaises(oss2.exceptions.InvalidArgument, self.bucket.put_bucket_lifecycle, BucketLifecycle([rule]))
# 转储为IA(天数大于object过期时间)
rule.storage_transitions = [oss2.models.StorageTransition(days=7,
storage_class=oss2.BUCKET_STORAGE_CLASS_IA)]
self.assertRaises(oss2.exceptions.InvalidArgument, self.bucket.put_bucket_lifecycle, BucketLifecycle([rule]))
# 转储为STANDARD
rule.storage_transitions = [oss2.models.StorageTransition(days=5,
storage_class=oss2.BUCKET_STORAGE_CLASS_STANDARD)]
self.assertRaises(oss2.exceptions.InvalidArgument, self.bucket.put_bucket_lifecycle, BucketLifecycle([rule]))
def test_put_lifecycle_days_less_than_transition_days(self):
from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle
rule = LifecycleRule(random_string(10), '中文前缀/',
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(days=3))
rule.storage_transitions = [oss2.models.StorageTransition(days=4, storage_class=oss2.BUCKET_STORAGE_CLASS_IA)]
self.assertRaises(oss2.exceptions.InvalidArgument, self.bucket.put_bucket_lifecycle, BucketLifecycle([rule]))
def test_lifecycle_storage_transitions_date(self):
from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle, StorageTransition
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)]
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.assertTrue(result.rules[0].tagging is None)
self.bucket.delete_bucket_lifecycle()
x-oss-request-id: 566B6BDB1BA604C27DD419B8'''
req_info = unittests.common.mock_response(do_request, response_text)
id = '中文ID'
prefix = '中文前缀'
status = 'Enabled'
days = 3
rule = LifecycleRule(id, prefix,
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(days=days),
storage_transitions=[StorageTransition(days=days, storage_class=oss2.BUCKET_STORAGE_CLASS_STANDARD)],
abort_multipart_upload=AbortMultipartUpload(days=days))
unittests.common.bucket().put_bucket_lifecycle(BucketLifecycle([rule]))
self.assertRequest(req_info, request_text.format(id, prefix, status, days, days, days))
并且如果用户创建文件abc/1.jpg,系统是不会创建abc/这个文件的,因此在删除abc/1.jpg后将不会再存在abc这个文件夹。
在后端存储的过程中不同的文件夹的文件仅仅是key值的前缀不一样。
所有文件的key值(这里需要通过prefix指定文件夹)
:param prefix: prefix for key,
:param days: save days
:return:
"""
logger.info("setting expired time started")
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
rule1 = LifecycleRule('rule1', prefix,
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(days=days))
lifecycle = BucketLifecycle([rule1])
result = bucket.put_bucket_lifecycle(lifecycle)
logger.info(" ".join((result.request_id, str(result.status))))
logger.info("setting expired time finished")
return result