def clean_and_delete_bucket(bucket): # check if bucket is in versioning status try: result = bucket.get_bucket_info() if result.versioning_status in [oss2.BUCKET_VERSIONING_ENABLE, oss2.BUCKET_VERSIONING_SUSPEND]: all_objects = bucket.list_object_versions() for obj in all_objects.versions: bucket.delete_object(obj.key, params={'versionId': obj.versionid}) except: pass # list all upload_parts to delete up_iter = oss2.MultipartUploadIterator(bucket) for up in up_iter: bucket.abort_multipart_upload(up.key, up.upload_id) # list all objects to delete obj_iter = oss2.ObjectIterator(bucket) for obj in obj_iter: bucket.delete_object(obj.key) # list all live channels to delete for ch_iter in oss2.LiveChannelIterator(bucket): bucket.delete_live_channel(ch_iter.name) # delete_bucket bucket.delete_bucket()
def test_upload_iterator(self): prefix = self.random_key('/') key = prefix + random_string(16) upload_list = [] dir_list = [] # 准备分片上传 for i in range(10): upload_list.append(self.bucket.init_multipart_upload(key).upload_id) # 准备碎片目录 for i in range(4): dir_list.append(prefix + random_string(5) + '/') self.bucket.init_multipart_upload(dir_list[-1] + random_string(5)) # 验证 uploads_got = [] dirs_got = [] for u in oss2.MultipartUploadIterator(self.bucket, prefix=prefix, delimiter='/', max_uploads=2): if u.is_prefix(): dirs_got.append(u.key) else: uploads_got.append(u.upload_id) self.assertEqual(sorted(upload_list), uploads_got) self.assertEqual(sorted(dir_list), dirs_got)
def test_multipart_iterator(self): # MultipartUploadIterator without payer setting, should be failed. access_err_flag = False try: up_iter = oss2.MultipartUploadIterator(self.payer_bucket) for up in up_iter: pass except oss2.exceptions.AccessDenied: access_err_flag = True self.assertEqual(access_err_flag, True) # MultipartUploadIterator with payer setting, should be successful. headers = dict() headers[OSS_REQUEST_PAYER] = "requester" up_iter = oss2.MultipartUploadIterator(self.payer_bucket, headers=headers) for up in up_iter: pass
def test_upload_iterator_chinese(self): upload_list = [] p = self.random_key() prefix_list = [p + '中文-阿+里-巴*巴', p + u'中文-四/十*大%盗'] for prefix in prefix_list: upload_list.append(self.bucket.init_multipart_upload(prefix).upload_id) uploads_got = [] for prefix in prefix_list: listed = list(oss2.MultipartUploadIterator(self.bucket, prefix=prefix, max_uploads=1)) uploads_got.append(listed[0].upload_id) self.assertEqual(sorted(upload_list), sorted(uploads_got))
def clean_and_delete_bucket(bucket): # check if bucket is in versioning status try: result = bucket.get_bucket_info() if result.versioning_status in [ oss2.BUCKET_VERSIONING_ENABLE, oss2.BUCKET_VERSIONING_SUSPEND ]: next_key_marker = None next_versionid_marker = None is_truncated = True while is_truncated is True: objects = bucket.list_object_versions( key_marker=next_key_marker, versionid_marker=next_versionid_marker) for obj in objects.versions: bucket.delete_object(obj.key, params={'versionId': obj.versionid}) for del_marker in objects.delete_marker: bucket.delete_object( del_marker.key, params={'versionId': del_marker.versionid}) is_truncated = objects.is_truncated if is_truncated: next_key_marker = objects.next_key_marker next_versionid_marker = objects.next_versionid_marker except: pass # list all upload_parts to delete up_iter = oss2.MultipartUploadIterator(bucket) for up in up_iter: bucket.abort_multipart_upload(up.key, up.upload_id) # list all objects to delete obj_iter = oss2.ObjectIterator(bucket) for obj in obj_iter: bucket.delete_object(obj.key) # list all live channels to delete for ch_iter in oss2.LiveChannelIterator(bucket): bucket.delete_live_channel(ch_iter.name) # delete_bucket bucket.delete_bucket()