Skip to content

Commit 9c1f6c3

Browse files
committed
support x-oss-complete-all: yes for CompleteMultipartUpload.
1 parent 59e85c6 commit 9c1f6c3

File tree

2 files changed

+117
-23
lines changed

2 files changed

+117
-23
lines changed

src/main/java/com/aliyun/oss/internal/OSSMultipartOperation.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.aliyun.oss.common.comm.ResponseHandler;
6565
import com.aliyun.oss.common.comm.ResponseMessage;
6666
import com.aliyun.oss.common.comm.ServiceClient;
67+
import com.aliyun.oss.common.comm.io.FixedLengthInputStream;
6768
import com.aliyun.oss.common.utils.CRC64;
6869
import com.aliyun.oss.common.utils.HttpUtil;
6970
import com.aliyun.oss.event.ProgressEventType;
@@ -169,18 +170,23 @@ public CompleteMultipartUploadResult completeMultipartUpload(
169170
parameters.put(UPLOAD_ID, uploadId);
170171

171172
List<PartETag> partETags = completeMultipartUploadRequest.getPartETags();
172-
Collections.sort(partETags, new Comparator<PartETag>() {
173-
@Override
174-
public int compare(PartETag p1, PartETag p2) {
175-
return p1.getPartNumber() - p2.getPartNumber();
176-
}
177-
});
173+
FixedLengthInputStream requestInstream;
174+
if (partETags != null) {
175+
Collections.sort(partETags, new Comparator<PartETag>() {
176+
@Override
177+
public int compare(PartETag p1, PartETag p2) {
178+
return p1.getPartNumber() - p2.getPartNumber();
179+
}
180+
});
181+
requestInstream = completeMultipartUploadRequestMarshaller.marshall(completeMultipartUploadRequest);
182+
} else {
183+
requestInstream = new FixedLengthInputStream(new ByteArrayInputStream("".getBytes()), 0);
184+
}
178185

179186
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(completeMultipartUploadRequest))
180187
.setMethod(HttpMethod.POST).setBucket(bucketName).setKey(key).setHeaders(headers)
181188
.setParameters(parameters)
182-
.setInputStreamWithLength(
183-
completeMultipartUploadRequestMarshaller.marshall(completeMultipartUploadRequest))
189+
.setInputStreamWithLength(requestInstream)
184190
.setOriginalRequest(completeMultipartUploadRequest).build();
185191

186192
List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
@@ -194,7 +200,9 @@ public int compare(PartETag p1, PartETag p2) {
194200
reponseHandlers);
195201
}
196202

197-
result.setClientCRC(calcObjectCRCFromParts(completeMultipartUploadRequest.getPartETags()));
203+
if (partETags != null) {
204+
result.setClientCRC(calcObjectCRCFromParts(partETags));
205+
}
198206
if (getInnerClient().getClientConfiguration().isCrcCheckEnabled()) {
199207
OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
200208
}

src/test/java/com/aliyun/oss/integrationtests/UploadPartTest.java

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,14 @@
3939
import java.util.ArrayList;
4040
import java.util.List;
4141

42+
import com.aliyun.oss.model.*;
4243
import junit.framework.Assert;
4344

4445
import org.junit.Ignore;
4546
import org.junit.Test;
4647

4748
import com.aliyun.oss.OSSErrorCode;
4849
import com.aliyun.oss.OSSException;
49-
import com.aliyun.oss.model.AbortMultipartUploadRequest;
50-
import com.aliyun.oss.model.Bucket;
51-
import com.aliyun.oss.model.CompleteMultipartUploadRequest;
52-
import com.aliyun.oss.model.CompleteMultipartUploadResult;
53-
import com.aliyun.oss.model.ListMultipartUploadsRequest;
54-
import com.aliyun.oss.model.ListPartsRequest;
55-
import com.aliyun.oss.model.MultipartUpload;
56-
import com.aliyun.oss.model.MultipartUploadListing;
57-
import com.aliyun.oss.model.OSSObject;
58-
import com.aliyun.oss.model.PartETag;
59-
import com.aliyun.oss.model.PartListing;
60-
import com.aliyun.oss.model.PartSummary;
61-
import com.aliyun.oss.model.UploadPartRequest;
62-
import com.aliyun.oss.model.UploadPartResult;
6350

6451
public class UploadPartTest extends TestBase {
6552

@@ -1120,4 +1107,103 @@ public void testDeleteAllBuckets() {
11201107
}
11211108
}
11221109

1110+
@Test
1111+
public void testNormalUploadWithEmptyPart() {
1112+
final String key = "normal-upload-empty-part-object";
1113+
final int partSize = 128 * 1024; //128KB
1114+
1115+
try {
1116+
String uploadId = claimUploadId(ossClient, bucketName, key);
1117+
List<PartETag> partETags = new ArrayList();
1118+
1119+
// Complete multipart upload
1120+
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
1121+
CompleteMultipartUploadResult completeMultipartUploadResult =
1122+
ossClient.completeMultipartUpload(completeMultipartUploadRequest);
1123+
1124+
} catch (Exception e) {
1125+
Assert.fail(e.getMessage());
1126+
}
1127+
}
1128+
1129+
@Test
1130+
public void testNormalUploadWithCompleteAllFlag() {
1131+
final String key1 = "normal-upload-empty-part-object-1";
1132+
final String key2 = "normal-upload-empty-part-object-2";
1133+
final int partSize1 = 128 * 1024; //128KB
1134+
final int partSize2 = 63 * 1024; //128KB
1135+
1136+
try {
1137+
String uploadId = claimUploadId(ossClient, bucketName, key1);
1138+
1139+
List<PartETag> partETags = new ArrayList<PartETag>();
1140+
1141+
// Upload part1 part
1142+
InputStream instream = genFixedLengthInputStream(partSize1);
1143+
UploadPartRequest uploadPartRequest = new UploadPartRequest();
1144+
uploadPartRequest.setBucketName(bucketName);
1145+
uploadPartRequest.setKey(key1);
1146+
uploadPartRequest.setInputStream(instream);
1147+
uploadPartRequest.setPartNumber(1);
1148+
uploadPartRequest.setPartSize(partSize1);
1149+
uploadPartRequest.setUploadId(uploadId);
1150+
UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
1151+
partETags.add(uploadPartResult.getPartETag());
1152+
1153+
// Upload part2 part
1154+
instream = genFixedLengthInputStream(partSize2);
1155+
uploadPartRequest = new UploadPartRequest();
1156+
uploadPartRequest.setBucketName(bucketName);
1157+
uploadPartRequest.setKey(key1);
1158+
uploadPartRequest.setInputStream(instream);
1159+
uploadPartRequest.setPartNumber(2);
1160+
uploadPartRequest.setPartSize(partSize2);
1161+
uploadPartRequest.setUploadId(uploadId);
1162+
uploadPartResult = ossClient.uploadPart(uploadPartRequest);
1163+
partETags.add(uploadPartResult.getPartETag());
1164+
1165+
// Complete multipart upload
1166+
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key1, uploadId, partETags);
1167+
CompleteMultipartUploadResult completeMultipartUploadResult1 = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
1168+
1169+
1170+
uploadId = claimUploadId(ossClient, bucketName, key2);
1171+
instream = genFixedLengthInputStream(partSize1);
1172+
uploadPartRequest = new UploadPartRequest();
1173+
uploadPartRequest.setBucketName(bucketName);
1174+
uploadPartRequest.setKey(key2);
1175+
uploadPartRequest.setInputStream(instream);
1176+
uploadPartRequest.setPartNumber(1);
1177+
uploadPartRequest.setPartSize(partSize1);
1178+
uploadPartRequest.setUploadId(uploadId);
1179+
ossClient.uploadPart(uploadPartRequest);
1180+
1181+
// Upload part2 part
1182+
instream = genFixedLengthInputStream(partSize2);
1183+
uploadPartRequest = new UploadPartRequest();
1184+
uploadPartRequest.setBucketName(bucketName);
1185+
uploadPartRequest.setKey(key2);
1186+
uploadPartRequest.setInputStream(instream);
1187+
uploadPartRequest.setPartNumber(2);
1188+
uploadPartRequest.setPartSize(partSize2);
1189+
uploadPartRequest.setUploadId(uploadId);
1190+
ossClient.uploadPart(uploadPartRequest);
1191+
1192+
completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key2, uploadId, null);
1193+
completeMultipartUploadRequest.addHeader("x-oss-complete-all", "yes");
1194+
CompleteMultipartUploadResult completeMultipartUploadResult2 = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
1195+
1196+
ObjectMetadata meta1 = ossClient.getObjectMetadata(bucketName, key1);
1197+
ObjectMetadata meta2 = ossClient.getObjectMetadata(bucketName, key2);
1198+
1199+
Assert.assertEquals(meta1.getContentLength(), meta2.getContentLength());
1200+
Assert.assertEquals(meta1.getServerCRC(), meta2.getServerCRC());
1201+
Assert.assertEquals(meta1.getETag(), meta2.getETag());
1202+
1203+
1204+
} catch (Exception e) {
1205+
Assert.fail(e.getMessage());
1206+
}
1207+
}
1208+
11231209
}

0 commit comments

Comments
 (0)