Search in sources :

Example 1 with Callback

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());
    }
}
Also used : Callback(com.aliyun.oss.model.Callback) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 2 with Callback

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());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) Callback(com.aliyun.oss.model.Callback) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 3 with Callback

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());
    }
}
Also used : Callback(com.aliyun.oss.model.Callback) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 4 with Callback

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());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) HashMap(java.util.HashMap) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) Date(java.util.Date) URL(java.net.URL) OSSException(com.aliyun.oss.OSSException) Callback(com.aliyun.oss.model.Callback) GeneratePresignedUrlRequest(com.aliyun.oss.model.GeneratePresignedUrlRequest) Test(org.junit.Test)

Example 5 with Callback

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());
    }
}
Also used : Callback(com.aliyun.oss.model.Callback) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

OSSException (com.aliyun.oss.OSSException)15 Callback (com.aliyun.oss.model.Callback)15 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)14 InputStream (java.io.InputStream)14 Test (org.junit.Test)14 OSSObject (com.aliyun.oss.model.OSSObject)13 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)8 PutObjectResult (com.aliyun.oss.model.PutObjectResult)8 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)6 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)6 PartETag (com.aliyun.oss.model.PartETag)6 UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)6 UploadPartResult (com.aliyun.oss.model.UploadPartResult)6 ArrayList (java.util.ArrayList)6 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 GeneratePresignedUrlRequest (com.aliyun.oss.model.GeneratePresignedUrlRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URL (java.net.URL)1