Skip to content

Commit bcf7d48

Browse files
liyanzhang505huiguangjun
authored andcommitted
1, Support sequential upload mode.
2, Support signed url with request payment.
1 parent 6e05a95 commit bcf7d48

File tree

8 files changed

+165
-16
lines changed

8 files changed

+165
-16
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ public InitiateMultipartUploadResult initiateMultipartUpload(
222222
Map<String, String> params = new HashMap<String, String>();
223223
params.put(SUBRESOURCE_UPLOADS, null);
224224

225+
Boolean sequentialMode = initiateMultipartUploadRequest.getSequentialMode();
226+
if (sequentialMode != null && sequentialMode.equals(true)) {
227+
params.put(SEQUENTIAL, null);
228+
}
229+
225230
// Set the request content to be empty (but not null) to avoid putting
226231
// parameters
227232
// to request body. Set HttpRequestFactory#createHttpRequest for

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ private void prepare(UploadCheckPoint uploadCheckPoint, UploadFileRequest upload
419419
initiateUploadRequest.setRequestPayer(payer);
420420
}
421421

422+
initiateUploadRequest.setSequentialMode(uploadFileRequest.getSequentialMode());
423+
422424
InitiateMultipartUploadResult initiateUploadResult = multipartOperation
423425
.initiateMultipartUpload(initiateUploadRequest);
424426
uploadCheckPoint.uploadID = initiateUploadResult.getUploadId();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public final class RequestParameters {
9191
public static final String MAX_PARTS = "max-parts";
9292
public static final String PART_NUMBER_MARKER = "part-number-marker";
9393
public static final String RULE_ID = "rule-id";
94+
public static final String SEQUENTIAL = "sequential";
9495

9596
public static final String SECURITY_TOKEN = "security-token";
9697

@@ -121,5 +122,6 @@ public final class RequestParameters {
121122
public static final String OSS_SIGNATURE = "x-oss-signature";
122123

123124
public static final String OSS_TRAFFIC_LIMIT = "x-oss-traffic-limit";
125+
public static final String OSS_REQUEST_PAYER = "x-oss-request-payer";
124126

125127
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SignParameters {
3838
SUBRESOURCE_UDF_IMAGE, SUBRESOURCE_UDF_IMAGE_DESC, SUBRESOURCE_UDF_APPLICATION, SUBRESOURCE_UDF_LOG,
3939
SUBRESOURCE_RESTORE, SUBRESOURCE_VRESIONS, SUBRESOURCE_VRESIONING, SUBRESOURCE_VRESION_ID,
4040
SUBRESOURCE_ENCRYPTION, SUBRESOURCE_POLICY, SUBRESOURCE_REQUEST_PAYMENT, OSS_TRAFFIC_LIMIT,
41-
SUBRESOURCE_QOS_INFO, SUBRESOURCE_ASYNC_FETCH});
41+
SUBRESOURCE_QOS_INFO, SUBRESOURCE_ASYNC_FETCH, SEQUENTIAL, OSS_REQUEST_PAYER});
4242

4343

4444
}

src/main/java/com/aliyun/oss/model/InitiateMultipartUploadRequest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class InitiateMultipartUploadRequest extends GenericRequest {
2727

2828
private ObjectMetadata objectMetadata;
29+
private Boolean sequentialMode;
2930

3031
/**
3132
* Constructor
@@ -74,4 +75,20 @@ public ObjectMetadata getObjectMetadata() {
7475
public void setObjectMetadata(ObjectMetadata objectMetadata) {
7576
this.objectMetadata = objectMetadata;
7677
}
78+
79+
80+
/**
81+
* Gets the sequential mode setting.
82+
*/
83+
public Boolean getSequentialMode() {
84+
return sequentialMode;
85+
}
86+
87+
/**
88+
* Sets upload in sequential mode or not.
89+
*/
90+
public void setSequentialMode(Boolean sequentialMode) {
91+
this.sequentialMode = sequentialMode;
92+
}
93+
7794
}

src/main/java/com/aliyun/oss/model/UploadFileRequest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ public int getTrafficLimit() {
136136
return trafficLimit;
137137
}
138138

139+
/**
140+
* Gets the sequential mode setting.
141+
*/
142+
public Boolean getSequentialMode() {
143+
return sequentialMode;
144+
}
145+
146+
/**
147+
* Sets upload in sequential mode or not.
148+
*/
149+
public void setSequentialMode(Boolean sequentialMode) {
150+
this.sequentialMode = sequentialMode;
151+
}
152+
139153
// Part size, by default it's 100KB.
140154
private long partSize = 1024 * 100;
141155
// Concurrent parts upload thread count. By default it's 1.
@@ -152,4 +166,6 @@ public int getTrafficLimit() {
152166
private Callback callback;
153167
// Traffic limit speed, its uint is bit/s
154168
private int trafficLimit;
169+
// Is Sequential mode or not.
170+
private Boolean sequentialMode;
155171
}

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

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,19 @@
2424
import java.io.File;
2525
import java.io.FileInputStream;
2626
import java.io.InputStream;
27-
import java.util.ArrayList;
28-
import java.util.HashMap;
29-
import java.util.List;
30-
import java.util.Map;
27+
import java.net.URL;
28+
import java.util.*;
29+
30+
import com.aliyun.oss.*;
31+
import com.aliyun.oss.internal.RequestParameters;
3132
import junit.framework.Assert;
3233
import org.junit.Test;
33-
import com.aliyun.oss.ClientConfiguration;
34-
import com.aliyun.oss.OSSClient;
3534
import com.aliyun.oss.common.auth.Credentials;
3635
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
3736
import com.aliyun.oss.common.auth.DefaultCredentials;
3837
import com.aliyun.oss.common.utils.BinaryUtil;
3938
import com.aliyun.oss.internal.OSSHeaders;
4039
import com.aliyun.oss.model.*;
41-
import com.aliyun.oss.OSSErrorCode;
42-
import com.aliyun.oss.OSSException;
4340
import com.aliyun.oss.utils.ResourceUtils;
4441

4542
public class ObjectRequestPaymentTest extends TestBase {
@@ -1099,4 +1096,62 @@ public void testDeleteObjectTagging() {
10991096
ossClient.deleteObject(bucketName, key);
11001097
}
11011098
}
1099+
1100+
@Test
1101+
public void testPutSignUrl() {
1102+
String key = "requestpayment-test-put-sign-url";
1103+
long inputStreamLength = 10;
1104+
InputStream instream = genFixedLengthInputStream(inputStreamLength);
1105+
1106+
Date date = new Date();
1107+
date.setTime(date.getTime() + 60 * 1000);
1108+
1109+
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT);
1110+
request.setExpiration(date);
1111+
1112+
Map<String, String> queryParam = new HashMap<String, String>();
1113+
queryParam.put(RequestParameters.OSS_REQUEST_PAYER, Payer.Requester.toString().toLowerCase());
1114+
request.setQueryParameter(queryParam);
1115+
1116+
URL signedUrl = ossPayerClient.generatePresignedUrl(request);
1117+
try {
1118+
ossPayerClient.putObject(signedUrl, instream, -1, null, true);
1119+
} catch (Exception e) {
1120+
e.printStackTrace();
1121+
Assert.fail(e.getMessage());
1122+
}
1123+
}
1124+
1125+
@Test
1126+
public void testGetSignUrl() {
1127+
String key = "requestpayment-test-get-sign-url";
1128+
InputStream instream = genFixedLengthInputStream(10);
1129+
try {
1130+
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
1131+
ossClient.putObject(putObjectRequest);
1132+
} catch (Exception e) {
1133+
e.printStackTrace();
1134+
Assert.fail(e.getMessage());
1135+
}
1136+
1137+
Date date = new Date();
1138+
date.setTime(date.getTime()+ 60*1000);
1139+
1140+
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
1141+
request.setExpiration(date);
1142+
1143+
Map<String, String> queryParam = new HashMap<String, String>();
1144+
queryParam.put(RequestParameters.OSS_REQUEST_PAYER, Payer.Requester.toString().toLowerCase());
1145+
request.setQueryParameter(queryParam);
1146+
1147+
URL signedUrl = ossPayerClient.generatePresignedUrl(request);
1148+
try {
1149+
ossPayerClient.getObject(signedUrl, null);
1150+
} catch (Exception e) {
1151+
e.printStackTrace();
1152+
Assert.fail(e.getMessage());
1153+
}
1154+
1155+
}
1156+
11021157
}

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

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
package com.aliyun.oss.integrationtests;
2121

2222
import java.io.File;
23+
24+
import com.aliyun.oss.ClientConfiguration;
25+
import com.aliyun.oss.OSS;
26+
import com.aliyun.oss.OSSClient;
27+
import com.aliyun.oss.common.auth.Credentials;
28+
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
29+
import com.aliyun.oss.common.auth.DefaultCredentials;
30+
import com.aliyun.oss.model.*;
2331
import junit.framework.Assert;
2432

2533
import org.junit.Test;
2634

27-
import com.aliyun.oss.model.GetObjectRequest;
28-
import com.aliyun.oss.model.ObjectListing;
29-
import com.aliyun.oss.model.ObjectMetadata;
30-
import com.aliyun.oss.model.UploadFileRequest;
31-
import com.aliyun.oss.model.UploadFileResult;
32-
3335
public class UploadFileTest extends TestBase {
3436

3537
@Test
@@ -72,7 +74,57 @@ public void testUploadFileWithoutCheckpoint() {
7274
ossClient.deleteBucket(bucketName);
7375
}
7476
}
75-
77+
78+
@Test
79+
public void testUploadFileSequential() {
80+
final String key = "obj-upload-file-Sequential";
81+
82+
String testBucketName = super.bucketName + "-upload-sequential";
83+
String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
84+
85+
//create client
86+
ClientConfiguration conf = new ClientConfiguration().setSupportCname(false);
87+
Credentials credentials = new DefaultCredentials(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
88+
OSS testOssClient = new OSSClient(endpoint, new DefaultCredentialProvider(credentials), conf);
89+
testOssClient.createBucket(testBucketName);
90+
91+
92+
try {
93+
File file = createSampleFile(key, 600 * 1024);
94+
95+
UploadFileRequest uploadFileRequest = new UploadFileRequest(testBucketName, key);
96+
uploadFileRequest.setUploadFile(file.getAbsolutePath());
97+
testOssClient.uploadFile(uploadFileRequest);
98+
99+
GetObjectRequest getObjectRequest = new GetObjectRequest(testBucketName, key);
100+
OSSObject ossObject = testOssClient.getObject(getObjectRequest);
101+
Assert.assertNull(ossObject.getResponse().getHeaders().get("Content-MD5"));
102+
} catch (Throwable e) {
103+
e.printStackTrace();
104+
Assert.fail(e.getMessage());
105+
}
106+
107+
try {
108+
File file = createSampleFile(key, 600 * 1024);
109+
110+
UploadFileRequest uploadFileRequest = new UploadFileRequest(testBucketName, key);
111+
uploadFileRequest.setUploadFile(file.getAbsolutePath());
112+
uploadFileRequest.setSequentialMode(true);
113+
114+
testOssClient.uploadFile(uploadFileRequest);
115+
116+
GetObjectRequest getObjectRequest = new GetObjectRequest(testBucketName, key);
117+
OSSObject ossObject = testOssClient.getObject(getObjectRequest);
118+
119+
Assert.assertNotNull(ossObject.getResponse().getHeaders().get("Content-MD5"));
120+
testOssClient.deleteObject(testBucketName, key);
121+
} catch (Throwable e) {
122+
e.printStackTrace();
123+
Assert.fail(e.getMessage());
124+
testOssClient.deleteBucket(testBucketName);
125+
}
126+
}
127+
76128
@Test
77129
public void testUploadFileWithCheckpoint() {
78130
final String key = "obj-upload-file-cp";

0 commit comments

Comments
 (0)