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_created_before_date(self):
from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle
rule = LifecycleRule(random_string(10), '中文前缀/',
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(created_before_date=datetime.date(2016, 12, 25)))
lifecycle = BucketLifecycle([rule])
self.bucket.put_bucket_lifecycle(lifecycle)
self.retry_assert(lambda: self.same_lifecycle(rule, self.bucket))
self.bucket.delete_bucket_lifecycle()
def test_lifecycle_abort_multipart_upload_days(self):
from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle, AbortMultipartUpload
rule = LifecycleRule(random_string(10), '中文前缀/',
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(created_before_date=datetime.date(2016, 12, 25)))
rule.abort_multipart_upload = AbortMultipartUpload(days=356)
lifecycle = BucketLifecycle([rule])
self.bucket.put_bucket_lifecycle(lifecycle)
self.retry_assert(lambda: self.same_lifecycle(rule, self.bucket))
result = self.bucket.get_bucket_lifecycle()
self.assertEqual(1, len(result.rules))
self.assertEqual(356, result.rules[0].abort_multipart_upload.days)
self.assertTrue(result.rules[0].tagging is None)
self.bucket.delete_bucket_lifecycle()
def test_lifecycle_versioning(self):
auth = oss2.Auth(OSS_ID, OSS_SECRET)
bucket_name = OSS_BUCKET + "-test-lifecycle-versioning"
bucket = oss2.Bucket(auth, self.endpoint, bucket_name)
bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE)
wait_meta_sync()
rule = LifecycleRule('rule1', 'test-prefix',
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(expired_detete_marker=True),
noncurrent_version_expiration = NoncurrentVersionExpiration(30),
noncurrent_version_sotrage_transitions =
[NoncurrentVersionStorageTransition(12, oss2.BUCKET_STORAGE_CLASS_IA),
NoncurrentVersionStorageTransition(20, oss2.BUCKET_STORAGE_CLASS_ARCHIVE)])
lifecycle = BucketLifecycle([rule])
bucket.put_bucket_lifecycle(lifecycle)
lifecycle = bucket.get_bucket_lifecycle()
self.assertEquals(1, len(lifecycle.rules))
self.assertEqual('rule1', lifecycle.rules[0].id)
self.assertEqual('test-prefix', lifecycle.rules[0].prefix)
self.assertEquals(LifecycleRule.ENABLED, lifecycle.rules[0].status)
self.assertEquals(True, lifecycle.rules[0].expiration.expired_detete_marker)
self.assertEquals(30, lifecycle.rules[0].noncurrent_version_expiration.noncurrent_days)
def test_lifecycle_days(self):
from oss2.models import LifecycleExpiration, LifecycleRule, BucketLifecycle
for prefix in ['中文前缀+/', '', u'中文前缀+/']:
rule = LifecycleRule(random_string(10), prefix,
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(days=356))
lifecycle = BucketLifecycle([rule])
self.bucket.put_bucket_lifecycle(lifecycle)
self.retry_assert(lambda: self.same_lifecycle(rule, self.bucket))
self.bucket.delete_bucket_lifecycle()
self.bucket.delete_bucket_lifecycle()
self.assertRaises(oss2.exceptions.NoSuchLifecycle, self.bucket.get_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))
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)
except oss2.exceptions.OssError:
self.assertFalse(True, "put lifecycle with tagging should fail ,but success")
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')
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))
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