Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:37 GMT
Content-Length: 0
Connection: keep-alive
x-oss-request-id: 566B6BD9B295345D15740F1F'''
id = 'hello world'
prefix = '中文前缀'
status = 'Disabled'
date = '2015-12-25T00:00:00.000Z'
date1 = datetime.date(2015, 12, 25)
req_info = unittests.common.mock_response(do_request, response_text)
rule = LifecycleRule(id, prefix,
status=LifecycleRule.DISABLED,
expiration=LifecycleExpiration(date=date1),
storage_transitions=[StorageTransition(created_before_date=date1, storage_class=oss2.BUCKET_STORAGE_CLASS_STANDARD)],
abort_multipart_upload=AbortMultipartUpload(created_before_date=date1))
unittests.common.bucket().put_bucket_lifecycle(BucketLifecycle([rule]))
self.assertRequest(req_info, request_text.format(id, prefix, status, date, date, date))
def test_lifecycle_object_tagging_exceptions_wrong_key(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()
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)
{0}{1}{2}{3}'''
response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:37 GMT
Content-Length: 0
Connection: keep-alive
x-oss-request-id: 566B6BD9B295345D15740F1F'''
id = 'hello world'
prefix = '中文前缀'
status = 'Disabled'
date = '2015-12-25T00:00:00.000Z'
req_info = mock_response(do_request, response_text)
rule = LifecycleRule(id, prefix,
status=LifecycleRule.DISABLED,
expiration=LifecycleExpiration(date=datetime.date(2015, 12, 25)))
bucket().put_bucket_lifecycle(BucketLifecycle([rule]))
self.assertRequest(req_info, request_text.format(id, prefix, status, date))
def test_lifecycle_object_tagging_exceptions_wrong_value(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()
tagging.tag_set.tagging_rule['test'] = 257*'a'
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)
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()
for rule in lifecycle.rules:
print rule.__dict__
self.assertEquals(5, len(lifecycle.rules))
bucket.delete_bucket()
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()
def test_put_lifecycle_invalid_transitions(self):
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,
def test_lifecycle_storage_transitions_days(self):
from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle, StorageTransition
rule = LifecycleRule(random_string(10), '中文前缀/',
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(days=357))
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(1, len(result.rules[0].storage_transitions))
self.assertEqual(356, result.rules[0].storage_transitions[0].days)
self.assertTrue(result.rules[0].tagging is None)
"""
关于prefix:
https://help.aliyun.com/knowledge_detail/39527.html
OSS中的文件夹其实是一个大小为0KB的空文件。因此,用户创建一个key值为1/的object就会定义文件夹1;
并且如果用户创建文件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