/
CallbackSample.java
91 lines (78 loc) · 4.02 KB
/
CallbackSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package samples;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Callback;
import com.aliyun.oss.model.Callback.CalbackBodyType;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
/**
* Examples about how to use the callback
*
*/
public class CallbackSample {
private static String endpoint = "*** Provide OSS endpoint ***";
private static String accessKeyId = "*** Provide your AccessKeyId ***";
private static String accessKeySecret = "*** Provide your AccessKeySecret ***";
private static String bucketName = "*** Provide bucket name ***";
// The callback url,for example: http://oss-demo.aliyuncs.com:23450或http://0.0.0.0:9090
// The service of that url must support the post method.
private static final String callbackUrl = "<yourCallbackServerUrl>";
public static void main(String[] args) throws IOException {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
String content = "Hello OSS";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, "key",
new ByteArrayInputStream(content.getBytes()));
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("{\\\"bucket\\\":${bucket},\\\"object\\\":${object},"
+ "\\\"mimeType\\\":${mimeType},\\\"size\\\":${size},"
+ "\\\"my_var1\\\":${x:var1},\\\"my_var2\\\":${x:var2}}");
callback.setCalbackBodyType(CalbackBodyType.JSON);
callback.addCallbackVar("x:var1", "value1");
callback.addCallbackVar("x:var2", "value2");
putObjectRequest.setCallback(callback);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
byte[] buffer = new byte[1024];
putObjectResult.getResponse().getContent().read(buffer);
putObjectResult.getResponse().getContent().close();
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorMessage());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} finally {
ossClient.shutdown();
}
}
}