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());
}
}
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());
}
}
}
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());
}
}
Aggregations