use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testPutObjectCallbackVar.
/**
* callback body type,type of callback body is json
*/
@Test
public void testPutObjectCallbackVar() throws Exception {
String key = "put-callback-var";
try {
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
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[bufferLength];
int nRead = putObjectResult.getResponse().getContent().read(buffer);
putObjectResult.getResponse().getContent().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
OSSObject obj = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, obj.getKey());
Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testMultipartUploadCallbackBody.
/**
* callback body has variables, type is url
*/
@Test
public void testMultipartUploadCallbackBody() {
String key = "multipart-upload-callback-body";
try {
String uploadId = claimUploadId(ossClient, bucketName, key);
InputStream instream = genFixedLengthInputStream(instreamLength);
List<PartETag> partETags = new ArrayList<PartETag>();
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(bucketName);
uploadPartRequest.setKey(key);
uploadPartRequest.setInputStream(instream);
uploadPartRequest.setPartNumber(1);
uploadPartRequest.setPartSize(instreamLength);
uploadPartRequest.setUploadId(uploadId);
UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
partETags.add(uploadPartResult.getPartETag());
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&my_var=${x:my_var}");
callback.setCalbackBodyType(CalbackBodyType.URL);
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
completeMultipartUploadRequest.setCallback(callback);
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
byte[] buffer = new byte[bufferLength];
int nRead = completeMultipartUploadResult.getResponse().getContent().read(buffer);
completeMultipartUploadResult.getResponse().getContent().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
OSSObject obj = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, obj.getKey());
Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
obj.forcedClose();
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testPutObjectCallbacURLChar.
/**
* callback body var has special characters, type is json.
*/
@Test
public void testPutObjectCallbacURLChar() throws Exception {
String key = "put-callback-url-char";
try {
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("春水碧于天,画船听雨眠。垆边人似月,皓腕凝霜雪。");
callback.setCalbackBodyType(CalbackBodyType.JSON);
callback.addCallbackVar("x:键值1", "值1:凌波不过横塘路,但目送,芳尘去。");
callback.addCallbackVar("x:键值2", "值2:长记曾携手处,千树压、西湖寒碧。");
putObjectRequest.setCallback(callback);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
byte[] buffer = new byte[bufferLength];
int nRead = putObjectResult.getCallbackResponseBody().read(buffer);
putObjectResult.getCallbackResponseBody().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
OSSObject obj = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, obj.getKey());
Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testGeneratePresignedUrlWithCallback.
@Test
public void testGeneratePresignedUrlWithCallback() {
String key = "generate-presigned-url-callback";
try {
// callback
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&" + "mimeType=${mimeType}&my_var1=${x:var1}&my_var2=${x:var2}");
callback.addCallbackVar("x:var1", "value1");
callback.addCallbackVar("x:var2", "value2");
callback.setCalbackBodyType(CalbackBodyType.URL);
// generate put url
Map<String, String> cbHeaders = new HashMap<String, String>();
OSSUtils.populateRequestCallback(cbHeaders, callback);
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT);
request.setExpiration(expiration);
request.setContentType("text/plain");
request.setHeaders(cbHeaders);
request.addHeader("x-oss-meta-author", "mingdi");
URL signedUrl = ossClient.generatePresignedUrl(request);
System.out.println("SignedUrl:" + signedUrl);
// put with url
Map<String, String> customHeaders = new HashMap<String, String>(cbHeaders);
customHeaders.put("Content-Type", "text/plain");
customHeaders.put("x-oss-meta-author", "mingdi");
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectResult putResult = ossClient.putObject(signedUrl, instream, instreamLength, customHeaders);
// check callback body
byte[] buffer = new byte[bufferLength];
int nRead = putResult.getCallbackResponseBody().read(buffer);
putResult.getCallbackResponseBody().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
// get object and check
OSSObject ossObject = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, ossObject.getKey());
Assert.assertEquals(instreamLength, ossObject.getObjectMetadata().getContentLength());
Assert.assertEquals("mingdi", ossObject.getObjectMetadata().getUserMetadata().get("author"));
ossObject.getObjectContent().close();
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testPutObjectCallbackBodyType.
/**
* callback body type,type of callback body is json
*/
@Test
public void testPutObjectCallbackBodyType() throws Exception {
String key = "put-callback-body-type";
try {
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
callback.setCalbackBodyType(CalbackBodyType.JSON);
putObjectRequest.setCallback(callback);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
byte[] buffer = new byte[bufferLength];
int nRead = putObjectResult.getCallbackResponseBody().read(buffer);
putObjectResult.getCallbackResponseBody().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
OSSObject obj = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, obj.getKey());
Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations