def test_delete_object_versions(self): key = 'requestpayment-test-delete-object-versions' content1 = 'test-content-1' content2 = 'test-content-2' result = self.owner_bucket.put_object(key, content1) versionid1 = result.versionid result = self.owner_bucket.put_object(key, content2) versionid2 = result.versionid version_list = BatchDeleteObjectVersionList() version_list.append( BatchDeleteObjectVersion(key=key, versionid=versionid1)) version_list.append( BatchDeleteObjectVersion(key=key, versionid=versionid2)) # Delete object verions without payer setting, should be failed. self.assertRaises(oss2.exceptions.AccessDenied, self.payer_bucket.delete_object_versions, version_list) # Delete object verions with payer setting, should be failed. headers = dict() headers[OSS_REQUEST_PAYER] = "requester" self.payer_bucket.delete_object_versions(version_list, headers=headers)
def test_batch_delete_same_object_multi_version(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-batch-delete-version" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() result = bucket.get_bucket_info() self.assertEqual(int(result.status)/100, 2) self.assertEqual(result.bucket_encryption_rule.sse_algorithm, None) self.assertEqual(result.versioning_status, "Enabled") # put version 1 result = bucket.put_object("test", "test1") self.assertEqual(int(result.status)/100, 2) self.assertTrue(result.versionid != "") versionid1 = result.versionid # put version 2 result = bucket.put_object("test", "test2") self.assertEqual(int(result.status)/100, 2) self.assertTrue(result.versionid != "") versionid2 = result.versionid version_list = BatchDeleteObjectVersionList() version_list.append(BatchDeleteObjectVersion(key="test", versionid=versionid1)) version_list.append(BatchDeleteObjectVersion(key="test", versionid=versionid2)) self.assertTrue(version_list.len(), 2) result = bucket.delete_object_versions(version_list) self.assertTrue(len(result.delete_versions) == 2) self.assertTrue(result.delete_versions[0].versionid == versionid1 or result.delete_versions[0].versionid == versionid2) self.assertTrue(result.delete_versions[1].versionid == versionid1 or result.delete_versions[1].versionid == versionid2) result = bucket.delete_object_versions(version_list) try: bucket.delete_bucket() except: self.assertFalse(True, "should not get a exception")
def test_resumable_download_with_version(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-resumable-download-with-version" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() key = "test_resumable_download_with_version-object" content_version1 = random_bytes(5 * 1024) content_version2 = random_bytes(5 * 1024 * 1024) # Put object version1 result = bucket.put_object(key, content_version1) versionid1 = result.versionid # Put object version2 result = bucket.put_object(key, content_version2) versionid2 = result.versionid # Resumable download object verison1, and check file length. filename = self.random_filename() oss2.resumable_download(bucket, key, filename, params={'versionId': versionid1}) self.assertFileContent(filename, content_version1) # Resumable download object verison2, and check file length. filename = self.random_filename() oss2.resumable_download(bucket, key, filename, params={'versionId': versionid2}) self.assertFileContent(filename, content_version2) version_list = BatchDeleteObjectVersionList() version_list.append(BatchDeleteObjectVersion(key, versionid1)) version_list.append(BatchDeleteObjectVersion(key, versionid2)) result = bucket.delete_object_versions(version_list) self.assertTrue(len(result.delete_versions) == 2) bucket.delete_bucket()
def test_resumable_download_with_version(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-resumable-download-with-version" bucket = oss2.Bucket(auth, OSS_ENDPOINT, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() self.version_bucket = bucket content_small = random_bytes(5 * 1024) result = bucket.put_object("object_small", content_small) version_small = result.versionid filename_small = self.random_filename() result = oss2.resumable_download(bucket, "object_small", filename_small) self.assertFileContent(filename_small, content_small) content_big = random_bytes(5 * 1024 * 1024) result = bucket.put_object("object_big", content_big) version_big = result.versionid filename_big = self.random_filename() result = oss2.resumable_download(bucket, "object_big", filename_big) self.assertFileContent(filename_big, content_big) version_list = BatchDeleteObjectVersionList() version_list.append( BatchDeleteObjectVersion("object_small", version_small)) version_list.append(BatchDeleteObjectVersion("object_big", version_big)) result = bucket.delete_object_versions(version_list) self.assertTrue(len(result.delete_versions) == 2) bucket.delete_bucket()
def test_delete_object_versions_with_invalid_arguments(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-delete-object-versions" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) config = BucketVersioningConfig() config.status = 'Enabled' bucket.put_bucket_versioning(config) wait_meta_sync() version_list = BatchDeleteObjectVersionList() version_list.append(BatchDeleteObjectVersion('test-key', '234')) self.assertRaises(oss2.exceptions.InvalidArgument, bucket.delete_object_versions, version_list) self.assertRaises(oss2.exceptions.ClientError, bucket.delete_object_versions, None) self.assertRaises(oss2.exceptions.ClientError, bucket.delete_object_versions, [])
def test_list_object_versions_truncated(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = random_string(63).lower() bucket = oss2.Bucket(auth, OSS_ENDPOINT, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() result = bucket.get_bucket_info() self.assertEqual(int(result.status) / 100, 2) self.assertEqual(result.bucket_encryption_rule.ssealgorithm, None) self.assertEqual(result.versioning_status, "Enabled") for i in range(0, 1024): bucket.put_object("test", "test" + str(i)) loop_time = 0 next_key_marker = '' next_version_marker = '' delete_versions = [] while True: result = bucket.list_object_versions( key_marker=next_key_marker, versionid_marker=next_version_marker) self.assertTrue(len(result.versions) > 0) self.assertTrue(len(result.delete_marker) == 0) version_list = BatchDeleteObjectVersionList() for item in result.versions: version_list.append( BatchDeleteObjectVersion(item.key, item.versionid)) delete_versions.append(version_list) if result.is_truncated: next_key_marker = result.next_key_marker next_version_marker = result.next_versionid_marker else: break loop_time += 1 if loop_time > 12: self.assertFalse(True, "loop too much times, break") for item in delete_versions: result = bucket.delete_object_versions(item) try: bucket.delete_bucket() except: self.assertFalse(True, "should not get a exception")
def test_upload_part_copy_with_versioning(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-upload-part-copy-with-versioning" bucket = oss2.Bucket(auth, OSS_ENDPOINT, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() result = bucket.get_bucket_info() self.assertEqual(int(result.status)/100, 2) self.assertEqual(result.bucket_encryption_rule.sse_algorithm, None) self.assertEqual(result.versioning_status, "Enabled") src_object = self.random_key() dst_object = self.random_key() content = random_bytes(200 * 1024) content2 = random_bytes(200 * 1024) # 上传源文件 version1 put_result1 = bucket.put_object(src_object, content) self.assertTrue(put_result1.versionid is not None) versionid1 = put_result1.versionid # 上传源文件 version2 put_result2 = bucket.put_object(src_object, content2) self.assertTrue(put_result2.versionid is not None) versionid2 = put_result2.versionid # part copy到目标文件 parts = [] upload_id = bucket.init_multipart_upload(dst_object).upload_id result = bucket.upload_part_copy(bucket_name, src_object, (0, 100 * 1024 - 1), dst_object, upload_id, 1) parts.append(oss2.models.PartInfo(1, result.etag)) result = bucket.upload_part_copy(bucket_name, src_object, (100*1024, None), dst_object, upload_id, 2, params={'versionId': versionid1}) parts.append(oss2.models.PartInfo(2, result.etag)) complete_result = bucket.complete_multipart_upload(dst_object, upload_id, parts) # 验证 content_got = bucket.get_object(dst_object).read() self.assertEqual(len(content_got), len(content)) self.assertTrue(content_got != content) version_list = BatchDeleteObjectVersionList() version_list.append(BatchDeleteObjectVersion(key=src_object, versionid=versionid1)) version_list.append(BatchDeleteObjectVersion(key=src_object, versionid=versionid2)) version_list.append(BatchDeleteObjectVersion(key=dst_object, versionid=complete_result.versionid)) self.assertTrue(version_list.len(), 3) result = bucket.delete_object_versions(version_list) try: bucket.delete_bucket() except: self.assertFalse(True, "should not get a exception")
def test_copy_object_with_version(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-copy-object-version" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() result = bucket.get_bucket_info() self.assertEqual(int(result.status) / 100, 2) self.assertEqual(result.bucket_encryption_rule.sse_algorithm, None) self.assertEqual(result.versioning_status, "Enabled") # put "test" version 1 result = bucket.put_object("test", "test1") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid1 = result.versionid # put "test" version 2 result = bucket.put_object("test", "test2") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid2 = result.versionid try: result_exception = bucket.copy_object(bucket_name, "test", "test_copy_wrong", params={"versionId": None}) self.assertFalse(True, "should get a exception") except: pass try: result_exception = bucket.copy_object(bucket_name, "test", "test_copy_wrong", params={"versionId": ''}) self.assertFalse(True, "should get a exception") except: pass try: result_exception = bucket.copy_object( bucket_name, "test", "test_copy_wrong", params={"versionId": 'NotExistVersionID'}) self.assertFalse(True, "should get a exception") except: pass result = bucket.copy_object(bucket_name, "test", "test_copy", params={'versionId': versionid1}) self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") copy_versionid = result.versionid version_list = BatchDeleteObjectVersionList() version_list.append( BatchDeleteObjectVersion(key="test", versionid=versionid1)) version_list.append( BatchDeleteObjectVersion(key="test", versionid=versionid2)) version_list.append( BatchDeleteObjectVersion(key="test_copy", versionid=copy_versionid)) self.assertTrue(version_list.len(), 3) result = bucket.delete_object_versions(version_list) try: bucket.delete_bucket() except: self.assertFalse(True, "should not get a exception")
def test_head_object_with_version(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-head-object-version" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() # put "test" version 1 result = bucket.put_object("test_no_version", "test") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid is None) try: result_exception = bucket.head_object( "test_no_version", params={"versionId": "IllegalVersion"}) self.assertFalse(True, "should get a exception") except: pass bucket.delete_object("test_no_version") config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() result = bucket.get_bucket_info() self.assertEqual(int(result.status) / 100, 2) self.assertEqual(result.bucket_encryption_rule.sse_algorithm, None) self.assertEqual(result.versioning_status, "Enabled") # put "test" version 1 result = bucket.put_object("test", "test1") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid1 = result.versionid # put "test" version 2 headers = {} headers['x-oss-storage-class'] = oss2.BUCKET_STORAGE_CLASS_ARCHIVE result = bucket.put_object("test", "test2", headers=headers) self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid2 = result.versionid try: result_exception = bucket.head_object("test", params={"versionId": None}) self.assertFalse(True, "should get a exception") except: pass try: result_normal = bucket.head_object("test", params={"versionId": ''}) self.assertFalse(True, "should get a exception") except: pass try: result_exception = bucket.head_object( "test_no_version", params={"versionId": "IllegalVersion"}) self.assertFalse(True, "should get a exception") except: pass try: result_exception = bucket.head_object( "test", params={ "versionId": "CAEQJhiBgIDVmYrr1RYiIGE5ZmUxMjViZDIwYjQwY2I5ODA1YWIxNmIyNDNjYjk4" }) self.assertFalse(True, "should get a exception") except: pass result1 = bucket.head_object("test", params={"versionId": versionid1}) result2 = bucket.head_object("test", params={"versionId": versionid2}) result3 = bucket.head_object("test") self.assertEqual(result2.versionid, result3.versionid) self.assertEqual(result1.object_type, result2.object_type) self.assertEqual(result1.content_type, result2.content_type) self.assertEqual(result1.content_length, result2.content_length) self.assertTrue(result1.etag != result2.etag) delete_result = bucket.delete_object("test") delete_marker_versionid = delete_result.versionid try: result3 = bucket.head_object( "test", params={'versionId': delete_marker_versionid}) self.assertFalse(True, "should get a exception, but not") except: pass version_list = BatchDeleteObjectVersionList() version_list.append( BatchDeleteObjectVersion(key="test", versionid=versionid1)) version_list.append( BatchDeleteObjectVersion(key="test", versionid=versionid2)) version_list.append( BatchDeleteObjectVersion(key="test", versionid=delete_marker_versionid)) self.assertTrue(version_list.len(), 3) result = bucket.delete_object_versions(version_list) try: bucket.delete_bucket() except: self.assertFalse(True, "should not get a exception")
def test_object_acl_with_version(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-object-acl-version" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() # put "test" result = bucket.put_object("test_no_version", "test1") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid is None) result = bucket.get_object_acl("test_no_version") bucket.delete_object("test_no_version") config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() result = bucket.get_bucket_info() self.assertEqual(int(result.status) / 100, 2) self.assertEqual(result.bucket_encryption_rule.sse_algorithm, None) self.assertEqual(result.versioning_status, "Enabled") # put "test" version 1 result = bucket.put_object("test", "test1") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid1 = result.versionid # put "test" version 2 result = bucket.put_object("test", "test2") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid2 = result.versionid try: result_exception = bucket.put_object_acl( "test", oss2.OBJECT_ACL_DEFAULT, params={'versionId': 'IllegalVersion'}) self.assertFalse(True, "should get a exception") except: pass try: result_exception = bucket.put_object_acl("test", oss2.OBJECT_ACL_DEFAULT, params={'versionId': ''}) self.assertFalse(True, "should get a exception") except: pass try: result_exception = bucket.get_object_acl( "test", params={'versionId': 'IllegalVersion'}) self.assertFalse(True, "should get a exception") except: pass try: result_exception = bucket.get_object_acl("test", params={'versionId': ''}) self.assertFalse(True, "should get a exception") except: pass result = bucket.get_object_acl("test", params={"versionId": versionid2}) self.assertEqual(result.acl, oss2.OBJECT_ACL_DEFAULT) result = bucket.put_object_acl("test", oss2.OBJECT_ACL_PUBLIC_READ, params={"versionId": versionid2}) self.assertEqual(int(result.status) / 100, 2) result = bucket.get_object_acl("test", params={"versionId": versionid2}) self.assertEqual(result.acl, oss2.OBJECT_ACL_PUBLIC_READ) version_list = BatchDeleteObjectVersionList() version_list.append( BatchDeleteObjectVersion(key="test", versionid=versionid1)) version_list.append( BatchDeleteObjectVersion(key="test", versionid=versionid2)) self.assertTrue(version_list.len(), 2) result = bucket.delete_object_versions(version_list) try: bucket.delete_bucket() except: self.assertFalse(True, "should not get a exception")
def test_batch_delete_objects_multi_version(self): from oss2.models import BucketVersioningConfig from oss2.models import BatchDeleteObjectVersion from oss2.models import BatchDeleteObjectVersionList auth = oss2.Auth(OSS_ID, OSS_SECRET) bucket_name = OSS_BUCKET + "-test-batch-delete-objects-version" bucket = oss2.Bucket(auth, self.endpoint, bucket_name) bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE) wait_meta_sync() config = BucketVersioningConfig() config.status = 'Enabled' result = bucket.put_bucket_versioning(config) wait_meta_sync() result = bucket.get_bucket_info() self.assertEqual(int(result.status) / 100, 2) self.assertEqual(result.bucket_encryption_rule.sse_algorithm, None) self.assertEqual(result.versioning_status, "Enabled") # put "test" version 1 result = bucket.put_object("test", "test1") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid1 = result.versionid # put "test" version 2 result = bucket.put_object("test", "test2") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid2 = result.versionid # put "foo" version 1 result = bucket.put_object("foo", "bar") self.assertEqual(int(result.status) / 100, 2) self.assertTrue(result.versionid != "") versionid2 = result.versionid result = bucket.list_object_versions() self.assertTrue(result.is_truncated == False) self.assertTrue(result.key_marker == '') self.assertTrue(result.versionid_marker == '') self.assertTrue(result.next_key_marker == '') self.assertTrue(result.next_versionid_marker == '') self.assertTrue(result.name == bucket_name) self.assertTrue(result.prefix == '') self.assertTrue(result.delimiter == '') self.assertTrue(len(result.delete_marker) == 0) self.assertTrue(len(result.versions) == 3) self.assertTrue(result.versions[0].key == "foo") self.assertTrue(result.versions[1].key == "test") # batch delete without version key_list = [] key_list.append("foo") key_list.append("test") result = bucket.batch_delete_objects(key_list) self.assertTrue(len(result.delete_versions) == 2) self.assertTrue(len(result.deleted_keys) == 2) self.assertTrue(result.delete_versions[0].delete_marker == True) self.assertTrue(result.delete_versions[1].delete_marker == True) result = bucket.list_object_versions() self.assertTrue(result.is_truncated == False) self.assertTrue(result.key_marker == '') self.assertTrue(result.versionid_marker == '') self.assertTrue(result.next_key_marker == '') self.assertTrue(result.next_versionid_marker == '') self.assertTrue(result.prefix == '') self.assertTrue(result.delimiter == '') self.assertTrue(len(result.delete_marker) == 2) self.assertTrue(len(result.versions) == 3) self.assertTrue(result.versions[0].key == "foo") self.assertTrue(result.versions[1].key == "test") version_list = BatchDeleteObjectVersionList() version_list.append( BatchDeleteObjectVersion(result.delete_marker[0].key, result.delete_marker[0].versionid)) version_list.append( BatchDeleteObjectVersion(result.delete_marker[1].key, result.delete_marker[1].versionid)) version_list.append( BatchDeleteObjectVersion(result.versions[0].key, result.versions[0].versionid)) version_list.append( BatchDeleteObjectVersion(result.versions[1].key, result.versions[1].versionid)) version_list.append( BatchDeleteObjectVersion(result.versions[2].key, result.versions[2].versionid)) result = bucket.delete_object_versions(version_list) self.assertTrue(len(result.delete_versions) == 5) try: bucket.delete_bucket() except: self.assertFalse(True, "should not get a exception")