Search in sources :

Example 6 with AppendObjectResult

use of com.aliyun.oss.model.AppendObjectResult 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 7 with AppendObjectResult

use of com.aliyun.oss.model.AppendObjectResult 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 8 with AppendObjectResult

use of com.aliyun.oss.model.AppendObjectResult 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

AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)8 AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)7 OSSObject (com.aliyun.oss.model.OSSObject)6 OSSException (com.aliyun.oss.OSSException)5 InputStream (java.io.InputStream)5 Test (org.junit.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)3 File (java.io.File)3 ClientException (com.aliyun.oss.ClientException)2 InconsistentException (com.aliyun.oss.InconsistentException)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