Skip to content

Commit 4f79cf3

Browse files
LYZhuiguangjun
authored andcommitted
Add request payment api & tests &examples
1 parent 2c81e6f commit 4f79cf3

26 files changed

+2315
-28
lines changed

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,23 @@ public DeleteVersionsResult deleteVersions(DeleteVersionsRequest deleteVersionsR
13451345
*/
13461346
public boolean doesObjectExist(String bucketName, String key, boolean isOnlyInOSS);
13471347

1348+
/**
1349+
* Checks if a specific {@link OSSObject} exists under the specific
1350+
* {@link Bucket}. 302 Redirect or OSS mirroring will not impact the result
1351+
* of this function.
1352+
*
1353+
* @param genericRequest
1354+
* A {@link GenericRequest} instance which specifies the bucket
1355+
* and object key.
1356+
* @param isOnlyInOSS
1357+
* true if ignore 302 redirect or mirroring; false if considering
1358+
* 302 redirect or mirroring, which could download the object
1359+
* from source to OSS when the file exists in source but is not
1360+
* in OSS yet.
1361+
* @return True if exists; false if not.
1362+
*/
1363+
public boolean doesObjectExist(GenericRequest genericRequest, boolean isOnlyInOSS) throws OSSException, ClientException;
1364+
13481365
/**
13491366
* Checks if a specific {@link OSSObject} exists.
13501367
*
@@ -3076,6 +3093,57 @@ public String generateRtmpUri(GenerateRtmpUriRequest generatePushflowUrlRequest)
30763093
*/
30773094
public GenericResult processObject(ProcessObjectRequest processObjectRequest) throws OSSException, ClientException;
30783095

3096+
/**
3097+
* Sets the request payment of the {@link Bucket}.
3098+
*
3099+
* @param bucketName
3100+
* The bucket name.
3101+
* @param payer
3102+
* The request payer setting
3103+
* @throws OSSException
3104+
* OSS Server side exception.
3105+
* @throws ClientException
3106+
* OSS Client side exception.
3107+
*/
3108+
public void setBucketRequestPayment(String bucketName, Payer payer) throws OSSException, ClientException;
3109+
3110+
/**
3111+
* Sets the request payment of the {@link Bucket}.
3112+
*
3113+
* @param setBucketRequestPaymentRequest
3114+
* A {@link SetBucketRequestPaymentRequest} instance that has
3115+
* the bucket name and payer setting.
3116+
* @throws OSSException
3117+
* OSS Server side exception.
3118+
* @throws ClientException
3119+
* OSS Client side exception.
3120+
*/
3121+
public void setBucketRequestPayment(SetBucketRequestPaymentRequest setBucketRequestPaymentRequest) throws OSSException, ClientException;
3122+
3123+
/**
3124+
* Gets the request payment of the {@link Bucket}.
3125+
*
3126+
* @param bucketName
3127+
* The bucket name.
3128+
* @throws OSSException
3129+
* OSS Server side exception.
3130+
* @throws ClientException
3131+
* OSS Client side exception.
3132+
*/
3133+
public GetBucketRequestPaymentResult getBucketRequestPayment(String bucketName) throws OSSException, ClientException;
3134+
3135+
/**
3136+
* Gets the request payment of the {@link Bucket}.
3137+
*
3138+
* @param genericRequest
3139+
* {@link GenericRequest} instance that has the bucket name.
3140+
* @throws OSSException
3141+
* OSS Server side exception.
3142+
* @throws ClientException
3143+
* OSS Client side exception.
3144+
*/
3145+
public GetBucketRequestPaymentResult getBucketRequestPayment(GenericRequest genericRequest) throws OSSException, ClientException;
3146+
30793147
/**
30803148
* Creates UDF
30813149
*

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ public boolean doesObjectExist(String bucketName, String key, boolean isOnlyInOS
660660
if (isOnlyInOSS) {
661661
return doesObjectExist(bucketName, key);
662662
} else {
663-
return objectOperation.doesObjectExistWithRedirect(bucketName, key);
663+
return objectOperation.doesObjectExistWithRedirect(new GenericRequest(bucketName, key));
664664
}
665665
}
666666

@@ -675,6 +675,15 @@ public boolean doesObjectExist(GenericRequest genericRequest) throws OSSExceptio
675675
return objectOperation.doesObjectExist(genericRequest);
676676
}
677677

678+
@Override
679+
public boolean doesObjectExist(GenericRequest genericRequest, boolean isOnlyInOSS) throws OSSException, ClientException {
680+
if (isOnlyInOSS) {
681+
return objectOperation.doesObjectExist(genericRequest);
682+
} else {
683+
return objectOperation.doesObjectExistWithRedirect(genericRequest);
684+
}
685+
}
686+
678687
@Override
679688
public void setObjectAcl(String bucketName, String key, CannedAccessControlList cannedACL)
680689
throws OSSException, ClientException {
@@ -1412,6 +1421,26 @@ public GenericResult processObject(ProcessObjectRequest processObjectRequest) th
14121421
return this.objectOperation.processObject(processObjectRequest);
14131422
}
14141423

1424+
@Override
1425+
public void setBucketRequestPayment(String bucketName, Payer payer) throws OSSException, ClientException {
1426+
this.bucketOperation.setBucketRequestPayment(new SetBucketRequestPaymentRequest(bucketName, payer));
1427+
}
1428+
1429+
@Override
1430+
public void setBucketRequestPayment(SetBucketRequestPaymentRequest setBucketRequestPaymentRequest) throws OSSException, ClientException{
1431+
this.bucketOperation.setBucketRequestPayment(setBucketRequestPaymentRequest);
1432+
}
1433+
1434+
@Override
1435+
public GetBucketRequestPaymentResult getBucketRequestPayment(String bucketName) throws OSSException, ClientException{
1436+
return this.bucketOperation.getBucketRequestPayment(new GenericRequest(bucketName));
1437+
}
1438+
1439+
@Override
1440+
public GetBucketRequestPaymentResult getBucketRequestPayment(GenericRequest genericRequest) throws OSSException, ClientException{
1441+
return this.bucketOperation.getBucketRequestPayment(genericRequest);
1442+
}
1443+
14151444
@Override
14161445
public void createUdf(CreateUdfRequest createUdfRequest) throws OSSException, ClientException {
14171446
this.udfOperation.createUdf(createUdfRequest);

src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public final class RequestMarshallers {
7878
public static final SetBucketVersioningRequestMarshaller setBucketVersioningRequestMarshaller = new SetBucketVersioningRequestMarshaller();
7979
public static final SetBucketEncryptionRequestMarshaller setBucketEncryptionRequestMarshaller = new SetBucketEncryptionRequestMarshaller();
8080
public static final SetBucketPolicyRequestMarshaller setBucketPolicyRequestMarshaller = new SetBucketPolicyRequestMarshaller();
81+
public static final SetBucketRequestPaymentRequestMarshaller setBucketRequestPaymentRequestMarshaller = new SetBucketRequestPaymentRequestMarshaller();
8182

8283
public static final CreateSelectObjectMetadataRequestMarshaller createSelectObjectMetadataRequestMarshaller = new CreateSelectObjectMetadataRequestMarshaller();
8384
public static final SelectObjectRequestMarshaller selectObjectRequestMarshaller = new SelectObjectRequestMarshaller();
@@ -945,6 +946,26 @@ public byte[] marshall(CreateLiveChannelRequest request) {
945946

946947
}
947948

949+
public static final class SetBucketRequestPaymentRequestMarshaller implements RequestMarshaller2<String> {
950+
951+
@Override
952+
public byte[] marshall(String payer) {
953+
StringBuffer xmlBody = new StringBuffer();
954+
xmlBody.append("<RequestPaymentConfiguration>");
955+
xmlBody.append("<Payer>" +payer + "</Payer>");
956+
xmlBody.append("</RequestPaymentConfiguration>");
957+
958+
byte[] rawData = null;
959+
try {
960+
rawData = xmlBody.toString().getBytes(DEFAULT_CHARSET_NAME);
961+
} catch (UnsupportedEncodingException e) {
962+
throw new ClientException("Unsupported encoding " + e.getMessage(), e);
963+
}
964+
return rawData;
965+
}
966+
967+
}
968+
948969
public static final class CreateUdfRequestMarshaller implements RequestMarshaller2<CreateUdfRequest> {
949970

950971
@Override

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

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketVersioningRequestMarshaller;
3737
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketEncryptionRequestMarshaller;
3838
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketPolicyRequestMarshaller;
39+
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketRequestPaymentRequestMarshaller;
3940
import static com.aliyun.oss.common.utils.CodingUtils.assertParameterNotNull;
4041
import static com.aliyun.oss.internal.OSSUtils.OSS_RESOURCE_MANAGER;
4142
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameValid;
@@ -65,6 +66,7 @@
6566
import static com.aliyun.oss.internal.ResponseParsers.getBucketImageProcessConfResponseParser;
6667
import static com.aliyun.oss.internal.ResponseParsers.getBucketEncryptionResponseParser;
6768
import static com.aliyun.oss.internal.ResponseParsers.getBucketPolicyResponseParser;
69+
import static com.aliyun.oss.internal.ResponseParsers.getBucketRequestPaymentResponseParser;
6870

6971
import java.io.ByteArrayInputStream;
7072
import java.util.ArrayList;
@@ -106,6 +108,7 @@
106108
import com.aliyun.oss.model.GenericRequest;
107109
import com.aliyun.oss.model.GetBucketImageResult;
108110
import com.aliyun.oss.model.GetBucketReplicationProgressRequest;
111+
import com.aliyun.oss.model.GetBucketRequestPaymentResult;
109112
import com.aliyun.oss.model.ImageProcess;
110113
import com.aliyun.oss.model.ReplicationRule;
111114
import com.aliyun.oss.model.ServerSideEncryptionConfiguration;
@@ -116,6 +119,7 @@
116119
import com.aliyun.oss.model.ListObjectsRequest;
117120
import com.aliyun.oss.model.ListVersionsRequest;
118121
import com.aliyun.oss.model.ObjectListing;
122+
import com.aliyun.oss.model.Payer;
119123
import com.aliyun.oss.model.PutBucketImageRequest;
120124
import com.aliyun.oss.model.PutImageStyleRequest;
121125
import com.aliyun.oss.model.SetBucketAclRequest;
@@ -124,6 +128,7 @@
124128
import com.aliyun.oss.model.SetBucketLoggingRequest;
125129
import com.aliyun.oss.model.SetBucketProcessRequest;
126130
import com.aliyun.oss.model.SetBucketRefererRequest;
131+
import com.aliyun.oss.model.SetBucketRequestPaymentRequest;
127132
import com.aliyun.oss.model.AddBucketReplicationRequest;
128133
import com.aliyun.oss.model.SetBucketStorageCapacityRequest;
129134
import com.aliyun.oss.model.SetBucketTaggingRequest;
@@ -408,8 +413,11 @@ public ObjectListing listObjects(ListObjectsRequest listObjectsRequest) throws O
408413
Map<String, String> params = new LinkedHashMap<String, String>();
409414
populateListObjectsRequestParameters(listObjectsRequest, params);
410415

416+
Map<String, String> headers = new HashMap<String, String>();
417+
populateRequestPayerHeader(headers, listObjectsRequest.getRequestPayer());
418+
411419
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
412-
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
420+
.setMethod(HttpMethod.GET).setBucket(bucketName).setHeaders(headers).setParameters(params)
413421
.setOriginalRequest(listObjectsRequest).build();
414422

415423
return doOperation(request, listObjectsReponseParser, bucketName, null, true);
@@ -429,8 +437,11 @@ public VersionListing listVersions(ListVersionsRequest listVersionsRequest) thro
429437
Map<String, String> params = new LinkedHashMap<String, String>();
430438
populateListVersionsRequestParameters(listVersionsRequest, params);
431439

440+
Map<String, String> headers = new HashMap<String, String>();
441+
populateRequestPayerHeader(headers, listVersionsRequest.getRequestPayer());
442+
432443
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
433-
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
444+
.setMethod(HttpMethod.GET).setBucket(bucketName).setHeaders(headers).setParameters(params)
434445
.setOriginalRequest(listVersionsRequest).build();
435446

436447
return doOperation(request, listVersionsReponseParser, bucketName, null, true);
@@ -1320,6 +1331,50 @@ public void deleteBucketPolicy(GenericRequest genericRequest) throws OSSExceptio
13201331
doOperation(request, emptyResponseParser, bucketName, null);
13211332
}
13221333

1334+
public void setBucketRequestPayment(SetBucketRequestPaymentRequest setBucketRequestPaymentRequest)
1335+
throws OSSException, ClientException {
1336+
1337+
assertParameterNotNull(setBucketRequestPaymentRequest, "setBucketRequestPaymentRequest");
1338+
assertParameterNotNull(setBucketRequestPaymentRequest.getPayer(), "setBucketRequestPaymentRequest.payer");
1339+
1340+
String bucketName = setBucketRequestPaymentRequest.getBucketName();
1341+
assertParameterNotNull(bucketName, "bucketName");
1342+
ensureBucketNameValid(bucketName);
1343+
1344+
Map<String, String> params = new HashMap<String, String>();
1345+
params.put(RequestParameters.SUBRESOURCE_REQUEST_PAYMENT, null);
1346+
1347+
Payer payer = setBucketRequestPaymentRequest.getPayer();
1348+
byte[] rawContent = setBucketRequestPaymentRequestMarshaller.marshall(payer.toString());
1349+
Map<String, String> headers = new HashMap<String, String>();
1350+
addRequestRequiredHeaders(headers, rawContent);
1351+
1352+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
1353+
.setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params).setHeaders(headers)
1354+
.setInputSize(rawContent.length).setInputStream(new ByteArrayInputStream(rawContent))
1355+
.setOriginalRequest(setBucketRequestPaymentRequest).build();
1356+
1357+
doOperation(request, emptyResponseParser, bucketName, null);
1358+
}
1359+
1360+
public GetBucketRequestPaymentResult getBucketRequestPayment(GenericRequest genericRequest) throws OSSException, ClientException {
1361+
1362+
assertParameterNotNull(genericRequest, "genericRequest");
1363+
1364+
String bucketName = genericRequest.getBucketName();
1365+
assertParameterNotNull(bucketName, "bucketName");
1366+
ensureBucketNameValid(bucketName);
1367+
1368+
Map<String, String> params = new HashMap<String, String>();
1369+
params.put(RequestParameters.SUBRESOURCE_REQUEST_PAYMENT, null);
1370+
1371+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
1372+
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
1373+
.setOriginalRequest(genericRequest).build();
1374+
1375+
return doOperation(request, getBucketRequestPaymentResponseParser, bucketName, null, true);
1376+
}
1377+
13231378
private static void populateListObjectsRequestParameters(ListObjectsRequest listObjectsRequest,
13241379
Map<String, String> params) {
13251380

@@ -1379,4 +1434,10 @@ private static void addOptionalACLHeader(Map<String, String> headers, CannedAcce
13791434
headers.put(OSSHeaders.OSS_CANNED_ACL, cannedAcl.toString());
13801435
}
13811436
}
1437+
1438+
private static void populateRequestPayerHeader (Map<String, String> headers, Payer payer) {
1439+
if (payer != null && payer.equals(Payer.Requester)) {
1440+
headers.put(OSSHeaders.OSS_REQUEST_PAYER, payer.toString().toLowerCase());
1441+
}
1442+
}
13821443
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.aliyun.oss.model.GetObjectRequest;
6161
import com.aliyun.oss.model.OSSObject;
6262
import com.aliyun.oss.model.ObjectMetadata;
63+
import com.aliyun.oss.model.Payer;
6364
import com.aliyun.oss.model.SimplifiedObjectMeta;
6465
import com.aliyun.oss.model.HeadObjectRequest;
6566

@@ -176,8 +177,17 @@ public int hashCode() {
176177
return result;
177178
}
178179

179-
public static ObjectStat getFileStat(OSSObjectOperation objectOperation, String bucketName, String key) {
180+
public static ObjectStat getFileStat(OSSObjectOperation objectOperation, DownloadFileRequest downloadFileRequest) {
181+
String bucketName = downloadFileRequest.getBucketName();
182+
String key = downloadFileRequest.getKey();
183+
180184
GenericRequest genericRequest = new GenericRequest(bucketName, key);
185+
186+
Payer payer = downloadFileRequest.getRequestPayer();
187+
if (payer != null) {
188+
genericRequest.setRequestPayer(payer);
189+
}
190+
181191
SimplifiedObjectMeta meta = objectOperation.getSimplifiedObjectMeta(genericRequest);
182192

183193
ObjectStat objStat = new ObjectStat();
@@ -430,8 +440,7 @@ private void prepare(DownloadCheckPoint downloadCheckPoint, DownloadFileRequest
430440
downloadCheckPoint.downloadFile = downloadFileRequest.getDownloadFile();
431441
downloadCheckPoint.bucketName = downloadFileRequest.getBucketName();
432442
downloadCheckPoint.objectKey = downloadFileRequest.getKey();
433-
downloadCheckPoint.objectStat = ObjectStat.getFileStat(objectOperation, downloadCheckPoint.bucketName,
434-
downloadCheckPoint.objectKey);
443+
downloadCheckPoint.objectStat = ObjectStat.getFileStat(objectOperation, downloadFileRequest);
435444
downloadCheckPoint.downloadParts = splitFile(downloadCheckPoint.objectStat.size,
436445
downloadFileRequest.getPartSize());
437446

@@ -565,6 +574,11 @@ public PartResult call() throws Exception {
565574
getObjectRequest.setUnmodifiedSinceConstraint(downloadFileRequest.getUnmodifiedSinceConstraint());
566575
getObjectRequest.setResponseHeaders(downloadFileRequest.getResponseHeaders());
567576
getObjectRequest.setRange(downloadPart.start, downloadPart.end);
577+
578+
Payer payer = downloadFileRequest.getRequestPayer();
579+
if(payer != null) {
580+
getObjectRequest.setRequestPayer(payer);
581+
}
568582

569583
OSSObject ossObj = objectOperation.getObject(getObjectRequest);
570584
objectMetadata = ossObj.getObjectMetadata();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,7 @@ public interface OSSHeaders extends HttpHeaders {
9292
static final String OSS_SELECT_INPUT_SPLIT_RANGE = OSS_SELECT_PREFIX + "-split-range";
9393

9494
static final String OSS_TAGGING = "x-oss-tagging";
95-
95+
96+
static final String OSS_REQUEST_PAYER = "x-oss-request-payer";
97+
9698
}

0 commit comments

Comments
 (0)