Skip to content

Commit b6d565d

Browse files
committed
support bucket transfer acceleration.
1 parent a204764 commit b6d565d

File tree

12 files changed

+372
-1
lines changed

12 files changed

+372
-1
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4193,4 +4193,37 @@ public VoidResult resizeUdfApplication(ResizeUdfApplicationRequest resizeUdfAppl
41934193
public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdfApplicationLogRequest)
41944194
throws OSSException, ClientException;
41954195

4196+
/**
4197+
* Set transferAcceleration configuration to the OSS Server
4198+
* @param bucketName
4199+
* @param enable
4200+
*
4201+
* @return A {@link VoidResult} instance wrapped void return and
4202+
* contains some basic response options, such as requestId.
4203+
*
4204+
* @throws OSSException
4205+
* @throws ClientException
4206+
*/
4207+
VoidResult setBucketTransferAcceleration(String bucketName, boolean enable) throws OSSException, ClientException;
4208+
4209+
/**
4210+
* Get transferAcceleration configuration from the OSS Server
4211+
* @param bucketName
4212+
* @return
4213+
* @throws OSSException
4214+
* @throws ClientException
4215+
*/
4216+
TransferAcceleration getBucketTransferAcceleration(String bucketName) throws OSSException, ClientException;
4217+
4218+
/**
4219+
* Delete transferAcceleration configuration from the OSS Server
4220+
* @param bucketName
4221+
*
4222+
* @return A {@link VoidResult} instance wrapped void return and
4223+
* contains some basic response options, such as requestId.
4224+
*
4225+
* @throws OSSException
4226+
* @throws ClientException
4227+
*/
4228+
VoidResult deleteBucketTransferAcceleration(String bucketName) throws OSSException, ClientException;
41964229
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,21 @@ public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdf
18161816
throw new ClientException("Not supported.");
18171817
}
18181818

1819+
@Override
1820+
public VoidResult setBucketTransferAcceleration(String bucketName, boolean enable) throws OSSException, ClientException {
1821+
return this.bucketOperation.setBucketTransferAcceleration(new SetBucketTransferAccelerationRequest(bucketName, enable));
1822+
}
1823+
1824+
@Override
1825+
public TransferAcceleration getBucketTransferAcceleration(String bucketName) throws OSSException, ClientException {
1826+
return this.bucketOperation.getBucketTransferAcceleration(new GenericRequest(bucketName));
1827+
}
1828+
1829+
@Override
1830+
public VoidResult deleteBucketTransferAcceleration(String bucketName) throws OSSException, ClientException {
1831+
return this.bucketOperation.deleteBucketTransferAcceleration(new GenericRequest(bucketName));
1832+
}
1833+
18191834
@Override
18201835
public void shutdown() {
18211836
try {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public final class RequestMarshallers {
9595
public static final DeleteBucketVpcipRequestMarshaller deleteBucketVpcipRequestMarshaller = new DeleteBucketVpcipRequestMarshaller();
9696

9797
public static final SetBucketResourceGroupRequestMarshaller setBucketResourceGroupRequestMarshaller = new SetBucketResourceGroupRequestMarshaller();
98+
public static final PutBucketTransferAccelerationRequestMarshaller putBucketTransferAccelerationRequestMarshaller = new PutBucketTransferAccelerationRequestMarshaller();
9899

99100
public interface RequestMarshaller<R> extends Marshaller<FixedLengthInputStream, R> {
100101

@@ -1691,6 +1692,24 @@ public byte[] marshall(RestoreObjectRequest request) {
16911692

16921693
}
16931694

1695+
public static final class PutBucketTransferAccelerationRequestMarshaller implements RequestMarshaller2<SetBucketTransferAccelerationRequest> {
1696+
@Override
1697+
public byte[] marshall(SetBucketTransferAccelerationRequest input) {
1698+
StringBuffer xmlBody = new StringBuffer();
1699+
xmlBody.append("<TransferAccelerationConfiguration><Enabled>");
1700+
xmlBody.append(input.isEnabled());
1701+
xmlBody.append("</Enabled></TransferAccelerationConfiguration>");
1702+
1703+
byte[] rawData = null;
1704+
try {
1705+
rawData = xmlBody.toString().getBytes(DEFAULT_CHARSET_NAME);
1706+
} catch (UnsupportedEncodingException e) {
1707+
throw new ClientException("Unsupported encoding " + e.getMessage(), e);
1708+
}
1709+
1710+
return rawData;
1711+
}
1712+
}
16941713

16951714
private static enum EscapedChar {
16961715
// "\r"

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import static com.aliyun.oss.common.parser.RequestMarshallers.extendBucketWormRequestMarshaller;
4848
import static com.aliyun.oss.common.parser.RequestMarshallers.initiateBucketWormRequestMarshaller;
4949
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketResourceGroupRequestMarshaller;
50+
import static com.aliyun.oss.common.parser.RequestMarshallers.putBucketTransferAccelerationRequestMarshaller;
5051
import static com.aliyun.oss.common.utils.CodingUtils.assertParameterNotNull;
5152
import static com.aliyun.oss.internal.OSSUtils.OSS_RESOURCE_MANAGER;
5253
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameValid;
@@ -92,6 +93,7 @@
9293
import static com.aliyun.oss.internal.ResponseParsers.initiateBucketWormResponseParser;
9394
import static com.aliyun.oss.internal.ResponseParsers.getBucketWormResponseParser;
9495
import static com.aliyun.oss.internal.ResponseParsers.getBucketResourceGroupResponseParser;
96+
import static com.aliyun.oss.internal.ResponseParsers.getBucketTransferAccelerationResponseParser;
9597

9698
import java.io.ByteArrayInputStream;
9799
import java.util.ArrayList;
@@ -198,6 +200,8 @@
198200
import com.aliyun.oss.model.InitiateBucketWormResult;
199201
import com.aliyun.oss.model.SetBucketResourceGroupRequest;
200202
import com.aliyun.oss.model.GetBucketResourceGroupResult;
203+
import com.aliyun.oss.model.TransferAcceleration;
204+
import com.aliyun.oss.model.SetBucketTransferAccelerationRequest;
201205

202206
/**
203207
* Bucket operation.
@@ -2036,6 +2040,58 @@ private static void addOptionalACLHeader(Map<String, String> headers, CannedAcce
20362040
}
20372041
}
20382042

2043+
public VoidResult setBucketTransferAcceleration(SetBucketTransferAccelerationRequest setBucketTransferAccelerationRequest) throws OSSException, ClientException {
2044+
assertParameterNotNull(setBucketTransferAccelerationRequest, "putBucketTransferAccelerationRequest");
2045+
2046+
String bucketName = setBucketTransferAccelerationRequest.getBucketName();
2047+
assertParameterNotNull(bucketName, "bucketName");
2048+
ensureBucketNameValid(bucketName);
2049+
2050+
Map<String, String> params = new HashMap<String, String>();
2051+
params.put(SUBRESOURCE_TRANSFER_ACCELERATION, null);
2052+
2053+
byte[] rawContent = putBucketTransferAccelerationRequestMarshaller.marshall(setBucketTransferAccelerationRequest);
2054+
2055+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
2056+
.setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params)
2057+
.setOriginalRequest(setBucketTransferAccelerationRequest).setInputSize(rawContent.length).setInputStream(new ByteArrayInputStream(rawContent)).build();
2058+
2059+
return doOperation(request, requestIdResponseParser, bucketName, null, true);
2060+
}
2061+
2062+
public TransferAcceleration getBucketTransferAcceleration(GenericRequest genericRequest) throws OSSException, ClientException {
2063+
assertParameterNotNull(genericRequest, "genericRequest");
2064+
2065+
String bucketName = genericRequest.getBucketName();
2066+
assertParameterNotNull(bucketName, "bucketName");
2067+
ensureBucketNameValid(bucketName);
2068+
2069+
Map<String, String> params = new HashMap<String, String>();
2070+
params.put(SUBRESOURCE_TRANSFER_ACCELERATION, null);
2071+
2072+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
2073+
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
2074+
.setOriginalRequest(genericRequest).build();
2075+
2076+
return doOperation(request, getBucketTransferAccelerationResponseParser, bucketName, null, true);
2077+
}
2078+
2079+
public VoidResult deleteBucketTransferAcceleration(GenericRequest genericRequest) throws OSSException, ClientException {
2080+
assertParameterNotNull(genericRequest, "genericRequest");
2081+
String bucketName = genericRequest.getBucketName();
2082+
assertParameterNotNull(bucketName, "bucketName");
2083+
ensureBucketNameValid(bucketName);
2084+
2085+
Map<String, String> params = new HashMap<String, String>();
2086+
params.put(SUBRESOURCE_TRANSFER_ACCELERATION, null);
2087+
2088+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
2089+
.setMethod(HttpMethod.DELETE).setBucket(bucketName).setParameters(params)
2090+
.setOriginalRequest(genericRequest).build();
2091+
2092+
return doOperation(request, requestIdResponseParser, bucketName, null);
2093+
}
2094+
20392095
private static void addOptionalHnsHeader(Map<String, String> headers, String hnsStatus) {
20402096
if (hnsStatus != null ) {
20412097
headers.put(OSSHeaders.OSS_HNS_STATUS, hnsStatus.toLowerCase());

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,5 @@ public final class RequestParameters {
143143
public static final String LIST_TYPE = "list-type";
144144
public static final String START_AFTER = "start-after";
145145
public static final String FETCH_OWNER = "fetch-owner";
146+
public static final String SUBRESOURCE_TRANSFER_ACCELERATION = "transferAcceleration";
146147
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
import com.aliyun.oss.model.VpcPolicy;
151151
import com.aliyun.oss.model.Vpcip;
152152
import com.aliyun.oss.model.GetBucketResourceGroupResult;
153+
import com.aliyun.oss.model.TransferAcceleration;
153154
/*
154155
* A collection of parsers that parse HTTP reponses into corresponding human-readable results.
155156
*/
@@ -191,6 +192,7 @@ public final class ResponseParsers {
191192
public static final InitiateBucketWormResponseParser initiateBucketWormResponseParser = new InitiateBucketWormResponseParser();
192193
public static final GetBucketWormResponseParser getBucketWormResponseParser = new GetBucketWormResponseParser();
193194
public static final GetBucketResourceGroupResponseParser getBucketResourceGroupResponseParser = new GetBucketResourceGroupResponseParser();
195+
public static final GetBucketTransferAccelerationResponseParser getBucketTransferAccelerationResponseParser = new GetBucketTransferAccelerationResponseParser();
194196

195197
public static final GetBucketInventoryConfigurationParser getBucketInventoryConfigurationParser = new GetBucketInventoryConfigurationParser();
196198
public static final ListBucketInventoryConfigurationsParser listBucketInventoryConfigurationsParser = new ListBucketInventoryConfigurationsParser();
@@ -3779,4 +3781,39 @@ public static GetBucketResourceGroupResult parseResourceGroupConfiguration(Input
37793781

37803782
}
37813783

3784+
public static final class GetBucketTransferAccelerationResponseParser implements ResponseParser<TransferAcceleration> {
3785+
@Override
3786+
public TransferAcceleration parse(ResponseMessage response) throws ResponseParseException {
3787+
try {
3788+
TransferAcceleration result = parseTransferAcceleration(response.getContent());
3789+
result.setRequestId(response.getRequestId());
3790+
3791+
return result;
3792+
} finally {
3793+
safeCloseResponse(response);
3794+
}
3795+
}
3796+
3797+
private TransferAcceleration parseTransferAcceleration(InputStream inputStream) throws ResponseParseException {
3798+
TransferAcceleration transferAcceleration = new TransferAcceleration(Boolean.FALSE);
3799+
if (inputStream == null) {
3800+
return transferAcceleration;
3801+
}
3802+
3803+
try {
3804+
Element root = getXmlRootElement(inputStream);
3805+
3806+
if (root.getChildText("Enabled") != null) {
3807+
transferAcceleration.setEnabled(Boolean.valueOf(root.getChildText("Enabled")));
3808+
}
3809+
3810+
return transferAcceleration;
3811+
} catch (JDOMParseException e) {
3812+
throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
3813+
} catch (Exception e) {
3814+
throw new ResponseParseException(e.getMessage(), e);
3815+
}
3816+
}
3817+
}
3818+
37823819
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public class SignParameters {
4141
SUBRESOURCE_QOS_INFO, SUBRESOURCE_ASYNC_FETCH, SEQUENTIAL, OSS_REQUEST_PAYER, VPCIP, VIP,
4242
SUBRESOURCE_INVENTORY, SUBRESOURCE_INVENTORY_ID, SUBRESOURCE_CONTINUATION_TOKEN, SUBRESOURCE_WORM,
4343
SUBRESOURCE_WORM_ID, SUBRESOURCE_WORM_EXTEND, SUBRESOURCE_CALLBACK, SUBRESOURCE_CALLBACK_VAR,
44-
SUBRESOURCE_DIR, SUBRESOURCE_RENAME, SUBRESOURCE_DIR_DELETE});
44+
SUBRESOURCE_DIR, SUBRESOURCE_RENAME, SUBRESOURCE_DIR_DELETE, SUBRESOURCE_TRANSFER_ACCELERATION});
4545

4646
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.aliyun.oss.model;
2+
3+
/**
4+
* Bucket TransferAcceleration Configuration
5+
*/
6+
public class SetBucketTransferAccelerationRequest extends GenericRequest {
7+
private TransferAcceleration transferAcceleration = new TransferAcceleration(false);
8+
9+
public SetBucketTransferAccelerationRequest(String bucketName, boolean enabled) {
10+
super();
11+
setBucketName(bucketName);
12+
setEnabled(enabled);
13+
}
14+
15+
public boolean isEnabled() {
16+
return transferAcceleration.isEnabled();
17+
}
18+
19+
public void setEnabled(boolean enabled) {
20+
transferAcceleration.setEnabled(enabled);
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.aliyun.oss.model;
2+
3+
import java.io.Serializable;
4+
5+
public class TransferAcceleration extends GenericResult implements Serializable {
6+
private static final long serialVersionUID = 2596858103633662949L;
7+
private boolean enabled;
8+
9+
public TransferAcceleration(boolean enabled) {
10+
super();
11+
setEnabled(enabled);
12+
}
13+
14+
public boolean isEnabled() {
15+
return enabled;
16+
}
17+
18+
public void setEnabled(boolean enabled) {
19+
this.enabled = enabled;
20+
}
21+
}

src/test/java/com/aliyun/oss/common/parser/RequestMarshallersTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,4 +1214,33 @@ public void testSetBucketResourceGroupRequestMarshaller() {
12141214
Assert.assertEquals(root.getChildText("ResourceGroupId"), "xxx-id-123");
12151215
}
12161216

1217+
@Test
1218+
public void testPutBucketTransferAccelerationRequestMarshaller() {
1219+
1220+
SetBucketTransferAccelerationRequest request = new SetBucketTransferAccelerationRequest("bucket", true);
1221+
Assert.assertEquals(request.isEnabled(), true);
1222+
request.setEnabled(false);
1223+
Assert.assertEquals(request.isEnabled(), false);
1224+
1225+
request.setEnabled(true);
1226+
Assert.assertEquals(request.isEnabled(), true);
1227+
1228+
byte[] data = putBucketTransferAccelerationRequestMarshaller.marshall(request);
1229+
ByteArrayInputStream is = new ByteArrayInputStream(data);
1230+
1231+
SAXBuilder builder = new SAXBuilder();
1232+
Document doc = null;
1233+
try {
1234+
doc = builder.build(is);
1235+
} catch (JDOMException e) {
1236+
e.printStackTrace();
1237+
} catch (IOException e) {
1238+
e.printStackTrace();
1239+
}
1240+
1241+
Element root = doc.getRootElement();
1242+
String status = root.getChildText("Enabled");
1243+
Assert.assertEquals("true", status);
1244+
}
1245+
12171246
}

0 commit comments

Comments
 (0)