Search in sources :

Example 6 with AppendObjectRequest

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

the class AppendObjectTest method testAppendObjectMissingArguments.

@Test
public void testAppendObjectMissingArguments() throws Exception {
    String key = "append-object-missing-arguments";
    final long instreamLength = 128 * 1024;
    try {
        Assert.assertEquals(true, ossClient.doesBucketExist(bucketName));
        InputStream instream = genFixedLengthInputStream(instreamLength);
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, instream, null);
        // Missing required parameter 'postition'
        ossClient.appendObject(appendObjectRequest);
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.MISSING_ARGUMENT, ex.getErrorCode());
        Assert.assertTrue(ex.getMessage().startsWith(MISSING_ARGUMENT_ERR));
    }
}
Also used : TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 7 with AppendObjectRequest

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

the class AppendObjectTest method testAppendObjectAtIllegalPosition.

@Test
public void testAppendObjectAtIllegalPosition() throws Exception {
    String key = "append-object-at-illlegal-position";
    final long instreamLength = 128 * 1024;
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, instream, null);
        appendObjectRequest.setPosition(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        OSSObject o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(instreamLength, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
        if (appendObjectResult.getNextPosition() != null) {
            Assert.assertEquals(instreamLength, appendObjectResult.getNextPosition().longValue());
        }
        Assert.assertEquals(appendObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
        o.getObjectContent().close();
        try {
            instream = genFixedLengthInputStream(instreamLength);
            appendObjectRequest = new AppendObjectRequest(bucketName, key, instream, null);
            // Set illegal postion to append, here should be 'instreamLength' rather than other positions.
            appendObjectRequest.setPosition(instreamLength - 1);
            ossClient.appendObject(appendObjectRequest);
        } catch (OSSException ex) {
            Assert.assertEquals(OSSErrorCode.POSITION_NOT_EQUAL_TO_LENGTH, ex.getErrorCode());
            Assert.assertTrue(ex.getMessage().startsWith(POSITION_NOT_EQUAL_TO_LENGTH_ERROR));
        }
    } 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) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) OSSException(com.aliyun.oss.OSSException) OSSException(com.aliyun.oss.OSSException) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) Test(org.junit.Test)

Example 8 with AppendObjectRequest

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

the class AppendObjectTest method testAppendExistingNormalObject.

@Test
public void testAppendExistingNormalObject() throws Exception {
    String key = "append-existing-normal-object";
    final long instreamLength = 128 * 1024;
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        ossClient.putObject(bucketName, key, instream, null);
        OSSObject o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(instreamLength, o.getObjectMetadata().getContentLength());
        o.getObjectContent().close();
        try {
            instream = genFixedLengthInputStream(instreamLength);
            AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, instream, null);
            appendObjectRequest.setPosition(instreamLength);
            ossClient.appendObject(appendObjectRequest);
        } catch (OSSException ex) {
            Assert.assertEquals(OSSErrorCode.OBJECT_NOT_APPENDALBE, ex.getErrorCode());
            Assert.assertTrue(ex.getMessage().startsWith(OBJECT_NOT_APPENDABLE_ERR));
        }
    } 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) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) OSSException(com.aliyun.oss.OSSException) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 9 with AppendObjectRequest

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

the class AppendObjectTest method testNormalAppendObject.

@Test
public void testNormalAppendObject() throws Exception {
    String key = "normal-append-object";
    final long instreamLength = 128 * 1024;
    for (int i = 0; i < 10; i++) {
        try {
            // Usage style  1
            InputStream instream = genFixedLengthInputStream(instreamLength);
            AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, instream, null);
            appendObjectRequest.setPosition(2 * i * instreamLength);
            AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
            OSSObject o = ossClient.getObject(bucketName, key);
            Assert.assertEquals(key, o.getKey());
            Assert.assertEquals((2 * i + 1) * instreamLength, o.getObjectMetadata().getContentLength());
            Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
            Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
            if (appendObjectResult.getNextPosition() != null) {
                Assert.assertEquals((2 * i + 1) * instreamLength, appendObjectResult.getNextPosition().longValue());
            }
            // Usage style 2
            final String filePath = genFixedLengthFile(instreamLength);
            appendObjectRequest = new AppendObjectRequest(bucketName, key, new File(filePath));
            appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
            appendObjectResult = ossClient.appendObject(appendObjectRequest);
            o = ossClient.getObject(bucketName, key);
            Assert.assertEquals(instreamLength * 2 * (i + 1), o.getObjectMetadata().getContentLength());
            Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
            if (appendObjectResult.getNextPosition() != null) {
                Assert.assertEquals(instreamLength * 2 * (i + 1), appendObjectResult.getNextPosition().longValue());
            }
            Assert.assertEquals(appendObjectResult.getRequestId().length(), REQUEST_ID_LEN);
            Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
        } 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) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) File(java.io.File) OSSException(com.aliyun.oss.OSSException) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) Test(org.junit.Test)

Example 10 with AppendObjectRequest

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

the class ObjectAclTest method testAppendObjectWithACLHeader.

@Test
public void testAppendObjectWithACLHeader() throws IOException {
    final String key = "append-object-with-acl-header";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    try {
        // Append at first
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setObjectAcl(CannedAccessControlList.PublicReadWrite);
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, instream, metadata);
        appendObjectRequest.setPosition(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        OSSObject o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
        if (appendObjectResult.getNextPosition() != null) {
            Assert.assertEquals(inputStreamLength, appendObjectResult.getNextPosition().longValue());
        }
        // Append at twice
        final String filePath = genFixedLengthFile(inputStreamLength);
        appendObjectRequest = new AppendObjectRequest(bucketName, key, new File(filePath));
        appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
        appendObjectResult = ossClient.appendObject(appendObjectRequest);
        o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(inputStreamLength * 2, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
        if (appendObjectResult.getNextPosition() != null) {
            Assert.assertEquals(inputStreamLength * 2, appendObjectResult.getNextPosition().longValue());
        }
        // Verify uploaded objects acl
        ObjectAcl returnedACL = ossClient.getObjectAcl(bucketName, key);
        Assert.assertEquals(ObjectPermission.PublicReadWrite, returnedACL.getPermission());
        Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) File(java.io.File) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) Test(org.junit.Test)

Aggregations

AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)11 Test (org.junit.Test)8 OSSException (com.aliyun.oss.OSSException)7 AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)7 OSSObject (com.aliyun.oss.model.OSSObject)7 InputStream (java.io.InputStream)7 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InconsistentException (com.aliyun.oss.InconsistentException)3 File (java.io.File)3 ClientException (com.aliyun.oss.ClientException)2 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)2 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)2 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)1 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)1 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)1 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)1 DeleteObjectsRequest (com.aliyun.oss.model.DeleteObjectsRequest)1