Search in sources :

Example 16 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testPutObjectCallbackParamInvalid.

/**
 * PutObject callback negative case,Invalid argument.
 */
@Test
public void testPutObjectCallbackParamInvalid() {
    String key = "put-callback-negative";
    // callbackUrl不合法,地址不合法、port不合法、超过5个
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl("my");
        callback.setCallbackBody("put-object-callback");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl("my.com:test");
        callback.setCallbackBody("put-object-callback");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl("my1.com;my2.com;my3.com;my4.com;my5.com;my6.com;");
        callback.setCallbackBody("put-object-callback");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    // callbackBody is empty
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    // callbackBody var format is invalid.
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("bucket=${bucket}&object=$(object)");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    // callback-var parameter's length is more than 5K
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("bucket=${bucket}&object=${object}");
        putObjectRequest.setCallback(callback);
        char[] bigArr = new char[1024];
        Arrays.fill(bigArr, 0, 1024, 'A');
        for (int i = 0; i < 10; i++) {
            callback.addCallbackVar("x:var" + i, new String(bigArr));
        }
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    // Callback failed. Expect return 203 (CallbackFailed).
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl("http://www.ifeng.com/");
        callback.setCallbackBody("put-object-callback");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.CALLBACK_FAILED, e.getErrorCode());
    }
}
Also used : Callback(com.aliyun.oss.model.Callback) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) OSSException(com.aliyun.oss.OSSException) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) Test(org.junit.Test)

Example 17 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testPutObjectCallbackBody.

/**
 * test variable in callback body,type of callback body is url.
 */
@Test
public void testPutObjectCallbackBody() throws Exception {
    String key = "put-callback-body";
    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("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);
        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());
        obj.close();
    } 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 18 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testPutObjectCallbackDefault.

/**
 * Testing default value settings. Only url and body are specified, others use default values.
 */
@Test
public void testPutObjectCallbackDefault() throws Exception {
    String key = "put-callback-default";
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("put-object-callback");
        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());
        obj.forcedClose();
    } 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 19 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testPutObjectCallbacJsonChar.

/**
 * callback body var has special characters, type is json.
 */
@Test
public void testPutObjectCallbacJsonChar() throws Exception {
    String key = "put-callback-json-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 20 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project aliyun-oss-java-sdk by aliyun.

the class GetObjectTest method testOverridedGetObject.

@Test
public void testOverridedGetObject() {
    final String key = "overrided-get-object";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    try {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, genFixedLengthInputStream(inputStreamLength), null);
        ossClient.putObject(putObjectRequest);
        // Override 1
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        OSSObject o = ossClient.getObject(getObjectRequest);
        Assert.assertEquals(bucketName, o.getBucketName());
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
        // Override 2
        o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(bucketName, o.getBucketName());
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
        // Override 3
        final String filePath = genFixedLengthFile(0);
        ObjectMetadata metadata = ossClient.getObject(getObjectRequest, new File(filePath));
        Assert.assertEquals(inputStreamLength, metadata.getContentLength());
        Assert.assertEquals(inputStreamLength, new File(filePath).length());
        metadata = ossClient.getObjectMetadata(bucketName, key);
        Assert.assertEquals(inputStreamLength, metadata.getContentLength());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) TestUtils.genRandomLengthFile(com.aliyun.oss.integrationtests.TestUtils.genRandomLengthFile) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) TestUtils.removeFile(com.aliyun.oss.integrationtests.TestUtils.removeFile) File(java.io.File) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)24 OSSException (com.aliyun.oss.OSSException)18 Test (org.junit.Test)14 OSSObject (com.aliyun.oss.model.OSSObject)13 PutObjectResult (com.aliyun.oss.model.PutObjectResult)9 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)8 Callback (com.aliyun.oss.model.Callback)8 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)8 InputStream (java.io.InputStream)8 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)7 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)7 IOException (java.io.IOException)6 ClientException (com.aliyun.oss.ClientException)5 OSS (com.aliyun.oss.OSS)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)3 ParseException (java.text.ParseException)3 TestUtils.genRandomLengthFile (com.aliyun.oss.integrationtests.TestUtils.genRandomLengthFile)2 TestUtils.removeFile (com.aliyun.oss.integrationtests.TestUtils.removeFile)2