From f4969a70d7a4f0ba6d30e50e1afb254d12ed1a85 Mon Sep 17 00:00:00 2001 From: chuanfengyu Date: Tue, 28 Apr 2020 09:05:11 +0800 Subject: [PATCH] add test case for losing data in multipart upload retry --- src/test/java/ObjectTest.java | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/test/java/ObjectTest.java b/src/test/java/ObjectTest.java index 2944d92..90e2dde 100644 --- a/src/test/java/ObjectTest.java +++ b/src/test/java/ObjectTest.java @@ -2151,6 +2151,48 @@ public void testUploadFileListHLAPI() throws AmazonServiceException, AmazonClien } catch (AmazonServiceException err) { AssertJUnit.assertEquals(err.getErrorCode(), "400 Bad Request"); } + } + + @Test(description = "multipart uploads losing data in retry operation, succeeds!") + public void testMultipartUploadRetryLosingData() { + String bucket_name = utils.getBucketName(prefix); + String key = "key1"; + String filePath = "./data/file.mpg"; + int fileSize = 23 * 1024 * 1024; + + svc.createBucket(new CreateBucketRequest(bucket_name)); + utils.createFile(filePath, fileSize); + + List partETags = new ArrayList(); + + InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket_name, key); + InitiateMultipartUploadResult initResponse = svc.initiateMultipartUpload(initRequest); + + File file = new File(filePath); + long contentLength = file.length(); + long partSize = 8 * 1024 * 1024; + + //multipart upload with retry + long filePosition = 0; + for (int i = 1; filePosition < contentLength; i++) { + partSize = Math.min(partSize, (contentLength - filePosition)); + UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(bucket_name).withKey(key) + .withUploadId(initResponse.getUploadId()).withPartNumber(i).withFileOffset(filePosition) + .withFile(file).withPartSize(partSize); + + partETags.add((PartETag) svc.uploadPart(uploadRequest).getPartETag()); + svc.uploadPart(uploadRequest);//Retry upload part + + filePosition += partSize; + } + + CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucket_name, key, + initResponse.getUploadId(), (List) partETags); + svc.completeMultipartUpload(compRequest); + + //get metadata + ObjectMetadata objectMeta = svc.getObjectMetadata(bucket_name, key); + AssertJUnit.assertEquals(fileSize, objectMeta.getContentLength()); } }