Skip to content

Commit 1d83498

Browse files
zhuxiaolong37huiguangjun
authored andcommitted
added async process object (#472)
1 parent 1069f2a commit 1d83498

File tree

14 files changed

+386
-16
lines changed

14 files changed

+386
-16
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4185,7 +4185,7 @@ public String generateRtmpUri(GenerateRtmpUriRequest generatePushflowUrlRequest)
41854185
* must be closed by the calller to release connection via calling
41864186
* getResponse().getContent().close().
41874187
* </p>
4188-
*
4188+
*
41894189
* @param processObjectRequest
41904190
* A {@link ProcessObjectRequest} instance that specifies the
41914191
* bucket name, the object key and the process (such as
@@ -5162,4 +5162,25 @@ public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdf
51625162
* If any errors occurred in OSS while processing the request.
51635163
*/
51645164
public VoidResult deleteBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException;
5165+
5166+
/**
5167+
* Apply async process on the specified image file.
5168+
* <p>
5169+
* The supported async process includes resize, rotate, crop, watermark, format,
5170+
* udf, customized style, etc.
5171+
* </p>
5172+
*
5173+
* @param asyncProcessObjectRequest
5174+
* A {@link AsyncProcessObjectRequest} instance that specifies the
5175+
* bucket name, the object key and the process (such as
5176+
* video/convert,f_mp4,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1)
5177+
* @return A {@link AsyncProcessObjectResult} instance which must be closed after the
5178+
* usage by the caller.
5179+
* @throws OSSException
5180+
* If any errors are encountered in the client while making the
5181+
* request or handling the response.
5182+
* @throws ClientException
5183+
* If any errors occurred in OSS while processing the request.
5184+
*/
5185+
public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asyncProcessObjectRequest) throws OSSException, ClientException;
51655186
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,11 @@ public VoidResult deleteBucketCallbackPolicy(GenericRequest genericRequest) thro
19771977
return this.bucketOperation.deleteBucketCallbackPolicy(genericRequest);
19781978
}
19791979

1980+
@Override
1981+
public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asyncProcessObjectRequest) throws OSSException, ClientException {
1982+
return this.objectOperation.asyncProcessObject(asyncProcessObjectRequest);
1983+
}
1984+
19801985
@Override
19811986
public void shutdown() {
19821987
try {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public final class RequestMarshallers {
101101
public static final PutBucketAccessMonitorRequestMarshaller putBucketAccessMonitorRequestMarshaller = new PutBucketAccessMonitorRequestMarshaller();
102102
public static final DoMetaQueryRequestMarshaller doMetaQueryRequestMarshaller = new DoMetaQueryRequestMarshaller();
103103
public static final SetBucketCallbackPolicyRequestMarshaller setBucketCallbackPolicyRequestMarshaller = new SetBucketCallbackPolicyRequestMarshaller();
104+
public static final AsyncProcessObjectRequestMarshaller asyncProcessObjectRequestMarshaller = new AsyncProcessObjectRequestMarshaller();
104105

105106
public interface RequestMarshaller<R> extends Marshaller<FixedLengthInputStream, R> {
106107

@@ -1913,6 +1914,26 @@ public byte[] marshall(SetBucketCallbackPolicyRequest request) {
19131914
}
19141915
}
19151916

1917+
public static final class AsyncProcessObjectRequestMarshaller implements RequestMarshaller2<AsyncProcessObjectRequest> {
1918+
1919+
@Override
1920+
public byte[] marshall(AsyncProcessObjectRequest request) {
1921+
StringBuffer processBody = new StringBuffer();
1922+
1923+
processBody.append(RequestParameters.X_OSS_ASYNC_PROCESS);
1924+
processBody.append("=" + request.getProcess());
1925+
1926+
byte[] rawData = null;
1927+
try {
1928+
rawData = processBody.toString().getBytes(DEFAULT_CHARSET_NAME);
1929+
} catch (UnsupportedEncodingException e) {
1930+
throw new ClientException("Unsupported encoding " + e.getMessage(), e);
1931+
}
1932+
return rawData;
1933+
}
1934+
1935+
}
1936+
19161937
private static enum EscapedChar {
19171938
// "\r"
19181939
RETURN("&#x000D;"),

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,36 @@ private static void populateGetObjectRequestHeaders(GetObjectRequest getObjectRe
12811281
populateTrafficLimitHeader(headers, getObjectRequest.getTrafficLimit());
12821282
}
12831283

1284+
public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asyncProcessObjectRequest) throws OSSException, ClientException {
1285+
1286+
assertParameterNotNull(asyncProcessObjectRequest, "asyncProcessObjectRequest");
1287+
1288+
String bucketName = asyncProcessObjectRequest.getBucketName();
1289+
String key = asyncProcessObjectRequest.getKey();
1290+
String process = asyncProcessObjectRequest.getProcess();
1291+
1292+
assertParameterNotNull(bucketName, "bucketName");
1293+
ensureBucketNameValid(bucketName);
1294+
assertParameterNotNull(key, "key");
1295+
ensureObjectKeyValid(key);
1296+
assertStringNotNullOrEmpty(process, "process");
1297+
1298+
Map<String, String> params = new HashMap<String, String>();
1299+
params.put(RequestParameters.X_OSS_ASYNC_PROCESS, null);
1300+
1301+
Map<String, String> headers = new HashMap<String, String>();
1302+
populateRequestPayerHeader(headers, asyncProcessObjectRequest.getRequestPayer());
1303+
1304+
byte[] rawContent = asyncProcessObjectRequestMarshaller.marshall(asyncProcessObjectRequest);
1305+
1306+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(asyncProcessObjectRequest))
1307+
.setMethod(HttpMethod.POST).setBucket(bucketName).setKey(key).setHeaders(headers).setParameters(params)
1308+
.setInputSize(rawContent.length).setInputStream(new ByteArrayInputStream(rawContent))
1309+
.setOriginalRequest(asyncProcessObjectRequest).build();
1310+
1311+
return doOperation(request, ResponseParsers.asyncProcessObjectResponseParser, bucketName, key, true);
1312+
}
1313+
12841314
private static void addDeleteObjectsRequiredHeaders(Map<String, String> headers, byte[] rawContent) {
12851315
headers.put(HttpHeaders.CONTENT_LENGTH, String.valueOf(rawContent.length));
12861316

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,6 @@ public final class RequestParameters {
157157

158158
public static final String SUBRESOURCE_REGION_LIST = "regionList";
159159
public static final String REGIONS = "regions";
160+
public static final String X_OSS_ASYNC_PROCESS = "x-oss-async-process";
161+
160162
}

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

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.aliyun.oss.common.parser.ResponseParser;
2525
import com.aliyun.oss.common.utils.DateUtil;
2626
import com.aliyun.oss.common.utils.HttpUtil;
27+
import com.aliyun.oss.common.utils.IOUtils;
2728
import com.aliyun.oss.common.utils.StringUtils;
2829
import com.aliyun.oss.internal.model.OSSErrorResult;
2930
import com.aliyun.oss.model.*;
@@ -36,6 +37,7 @@
3637
import com.aliyun.oss.model.LiveChannelStat.AudioStat;
3738
import com.aliyun.oss.model.LiveChannelStat.VideoStat;
3839
import com.aliyun.oss.model.SetBucketCORSRequest.CORSRule;
40+
import org.codehaus.jettison.json.JSONObject;
3941
import org.jdom2.Document;
4042
import org.jdom2.Element;
4143
import org.jdom2.input.JDOMParseException;
@@ -137,6 +139,7 @@ public final class ResponseParsers {
137139
public static final DoMetaQueryResponseParser doMetaQueryResponseParser = new DoMetaQueryResponseParser();
138140
public static final DescribeRegionsResponseParser describeRegionsResponseParser = new DescribeRegionsResponseParser();
139141
public static final GetBucketCallbackPolicyResponseParser getBucketCallbackPolicyResponseParser = new GetBucketCallbackPolicyResponseParser();
142+
public static final AsyncProcessObjectResponseParser asyncProcessObjectResponseParser = new AsyncProcessObjectResponseParser();
140143

141144
public static Long parseLongWithDefault(String defaultValue){
142145
if(defaultValue == null || "".equals(defaultValue)){
@@ -4232,23 +4235,55 @@ public GetBucketCallbackPolicyResult parse(ResponseMessage response) throws Resp
42324235
}
42334236

42344237
private GetBucketCallbackPolicyResult parseGetBucketCallbackPolicy(InputStream inputStream) throws ResponseParseException {
4235-
GetBucketCallbackPolicyResult result = new GetBucketCallbackPolicyResult();
4236-
if (inputStream == null) {
4237-
return result;
4238+
GetBucketCallbackPolicyResult result = new GetBucketCallbackPolicyResult();
4239+
if (inputStream == null) {
4240+
return result;
4241+
}
4242+
4243+
try {
4244+
Element root = getXmlRootElement(inputStream);
4245+
4246+
List<Element> fileElem = root.getChildren();
4247+
List<PolicyCallbackItem> policyCallbackItems = new ArrayList<PolicyCallbackItem>();
4248+
for (Element elem : fileElem) {
4249+
PolicyCallbackItem policyCallbackItem = new PolicyCallbackItem(elem.getChildText("PolicyName"), elem.getChildText("Callback"));
4250+
policyCallbackItem.setCallbackVar(elem.getChildText("CallbackVar"));
4251+
policyCallbackItems.add(policyCallbackItem);
42384252
}
4253+
result.setPolicyCallbackItems(policyCallbackItems);
4254+
return result;
4255+
} catch (Exception e) {
4256+
throw new ResponseParseException(e.getMessage(), e);
4257+
}
4258+
}
4259+
}
42394260

4240-
try {
4241-
Element root = getXmlRootElement(inputStream);
4261+
public static final class AsyncProcessObjectResponseParser implements ResponseParser<AsyncProcessObjectResult> {
42424262

4243-
List<Element> fileElem = root.getChildren();
4244-
List<PolicyCallbackItem> policyCallbackItems = new ArrayList<PolicyCallbackItem>();
4245-
for(Element elem : fileElem){
4246-
PolicyCallbackItem policyCallbackItem = new PolicyCallbackItem(elem.getChildText("PolicyName"), elem.getChildText("Callback"));
4247-
policyCallbackItem.setCallbackVar(elem.getChildText("CallbackVar"));
4248-
policyCallbackItems.add(policyCallbackItem);
4249-
}
4250-
result.setPolicyCallbackItems(policyCallbackItems);
4251-
return result;
4263+
@Override
4264+
public AsyncProcessObjectResult parse(ResponseMessage response) throws ResponseParseException {
4265+
try {
4266+
AsyncProcessObjectResult result = parseAsyncProcessObject(response.getContent());
4267+
setResultParameter(result, response);
4268+
return result;
4269+
} finally {
4270+
safeCloseResponse(response);
4271+
}
4272+
}
4273+
4274+
private AsyncProcessObjectResult parseAsyncProcessObject(InputStream inputStream) throws ResponseParseException {
4275+
AsyncProcessObjectResult asyncProcessObjectResult = new AsyncProcessObjectResult();
4276+
if (inputStream == null) {
4277+
return asyncProcessObjectResult;
4278+
}
4279+
4280+
try {
4281+
String jsonStr = IOUtils.readStreamAsString(inputStream, "UTF-8");
4282+
JSONObject jsonObject = new JSONObject(jsonStr);
4283+
asyncProcessObjectResult.setAsyncRequestId(jsonObject.getString("RequestId"));
4284+
asyncProcessObjectResult.setEventId(jsonObject.getString("EventId"));
4285+
asyncProcessObjectResult.setTaskId(jsonObject.getString("TaskId"));
4286+
return asyncProcessObjectResult;
42524287
} catch (Exception e) {
42534288
throw new ResponseParseException(e.getMessage(), e);
42544289
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public class SignParameters {
4343
SUBRESOURCE_WORM_ID, SUBRESOURCE_WORM_EXTEND, SUBRESOURCE_CALLBACK, SUBRESOURCE_CALLBACK_VAR,
4444
SUBRESOURCE_DIR, SUBRESOURCE_RENAME, SUBRESOURCE_DIR_DELETE, SUBRESOURCE_TRANSFER_ACCELERATION,
4545
X_OSS_AC_SOURCE_IP, X_OSS_AC_SUBNET_MASK, X_OSS_AC_VPC_ID, X_OSS_AC_FORWARD_ALLOW, META_QUERY, SUBRESOURCE_RESOURCE_GROUP,
46-
SUBRESOURCE_REGION_LIST});
46+
SUBRESOURCE_REGION_LIST, X_OSS_ASYNC_PROCESS});
4747

4848
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.aliyun.oss.model;
2+
3+
4+
public class AsyncProcessObjectRequest extends GenericRequest {
5+
6+
public AsyncProcessObjectRequest(String bucketName, String key, String process) {
7+
super(bucketName, key);
8+
this.process = process;
9+
}
10+
11+
public String getProcess() {
12+
return process;
13+
}
14+
15+
public void setProcess(String process) {
16+
this.process = process;
17+
}
18+
19+
public AsyncProcessObjectRequest withProcess(String process) {
20+
this.process = process;
21+
return this;
22+
}
23+
24+
private String process;
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.aliyun.oss.model;
2+
3+
public class AsyncProcessObjectResult extends GenericResult {
4+
5+
private String asyncRequestId;
6+
private String eventId;
7+
private String taskId;
8+
9+
public String getAsyncRequestId() {
10+
return asyncRequestId;
11+
}
12+
13+
public void setAsyncRequestId(String asyncRequestId) {
14+
this.asyncRequestId = asyncRequestId;
15+
}
16+
17+
public String getEventId() {
18+
return eventId;
19+
}
20+
21+
public void setEventId(String eventId) {
22+
this.eventId = eventId;
23+
}
24+
25+
public String getTaskId() {
26+
return taskId;
27+
}
28+
29+
public void setTaskId(String taskId) {
30+
this.taskId = taskId;
31+
}
32+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package samples;
2+
3+
import com.aliyun.oss.common.utils.BinaryUtil;
4+
import com.aliyun.oss.model.*;
5+
6+
public class AsyncProcessObjectSample {
7+
private static String endpoint = "<endpoint, http://oss-cn-hangzhou.aliyuncs.com>";
8+
private static String accessKeyId = "<accessKeyId>";
9+
private static String accessKeySecret = "<accessKeySecret>";
10+
private static String bucketName = "<bucketName>";
11+
private static final String saveAsKey = "<syncSaveObjectName>";
12+
private static final String key = "<objectName>";
13+
14+
public static void main(String[] args) {
15+
/*
16+
* Constructs a client instance with your account for accessing OSS
17+
*/
18+
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
19+
try {
20+
StringBuilder styleBuilder = new StringBuilder();
21+
styleBuilder.append("video/convert,f_mp4,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1"); // resize
22+
styleBuilder.append("|sys/saveas,");
23+
styleBuilder.append("o_" + BinaryUtil.toBase64String(saveAsKey.getBytes()).replaceAll("=", ""));
24+
styleBuilder.append(",");
25+
styleBuilder.append("b_" + BinaryUtil.toBase64String(bucketName.getBytes()).replaceAll("=", ""));
26+
27+
AsyncProcessObjectRequest request = new AsyncProcessObjectRequest(bucketName, key, styleBuilder.toString());
28+
29+
AsyncProcessObjectResult asyncProcessObject = ossClient.asyncProcessObject(request);
30+
System.out.println(asyncProcessObject.getAsyncRequestId());
31+
System.out.println(asyncProcessObject.getEventId());
32+
System.out.println(asyncProcessObject.getTaskId());
33+
} catch (OSSException oe) {
34+
System.out.println("Caught an OSSException, which means your request made it to OSS, "
35+
+ "but was rejected with an error response for some reason.");
36+
System.out.println("Error Message: " + oe.getMessage());
37+
System.out.println("Error Code: " + oe.getErrorCode());
38+
System.out.println("Request ID: " + oe.getRequestId());
39+
System.out.println("Host ID: " + oe.getHostId());
40+
} catch (ClientException ce) {
41+
System.out.println("Caught an ClientException, which means the client encountered "
42+
+ "a serious internal problem while trying to communicate with OSS, "
43+
+ "such as not being able to access the network.");
44+
System.out.println("Error Message: " + ce.getMessage());
45+
} finally {
46+
/*
47+
* Do not forget to shut down the client finally to release all allocated resources.
48+
*/
49+
ossClient.shutdown();
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)