use of com.aliyun.oss.model.CopyObjectResult in project aliyun-oss-java-sdk by aliyun.
the class ObjectAclTest method testCopyObjectWithACLHeader.
@Test
public void testCopyObjectWithACLHeader() throws IOException {
final String sourceBucket = "copy-existing-object-source-bucket";
final String targetBucket = "copy-existing-object-target-bucket";
final String sourceKey = "copy-existing-object-source-object";
final String targetKey = "copy-existing-object-target-object";
final String userMetaKey0 = "user";
final String userMetaValue0 = "aliy";
final String userMetaKey1 = "tag";
final String userMetaValue1 = "copy-object";
final String contentType = "application/txt";
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
byte[] content = { 'A', 'l', 'i', 'y', 'u', 'n' };
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(content.length);
metadata.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
metadata.addUserMetadata(userMetaKey0, userMetaValue0);
PutObjectResult putObjectResult = ossClient.putObject(sourceBucket, sourceKey, new ByteArrayInputStream(content), metadata);
ObjectMetadata newObjectMetadata = new ObjectMetadata();
newObjectMetadata.setContentLength(content.length);
newObjectMetadata.setContentType(contentType);
newObjectMetadata.addUserMetadata(userMetaKey1, userMetaValue1);
newObjectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
copyObjectRequest.setNewObjectMetadata(newObjectMetadata);
CopyObjectResult copyObjectResult = ossClient.copyObject(copyObjectRequest);
String sourceETag = putObjectResult.getETag();
String targetETag = copyObjectResult.getETag();
Assert.assertEquals(sourceETag, targetETag);
Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
OSSObject ossObject = ossClient.getObject(targetBucket, targetKey);
newObjectMetadata = ossObject.getObjectMetadata();
Assert.assertEquals(contentType, newObjectMetadata.getContentType());
Assert.assertEquals(userMetaValue1, newObjectMetadata.getUserMetadata().get(userMetaKey1));
// Verify uploaded objects acl
ObjectAcl returnedACL = ossClient.getObjectAcl(targetBucket, targetKey);
Assert.assertEquals(ObjectPermission.PublicRead, returnedACL.getPermission());
Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
waitForCacheExpiration(5);
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.CopyObjectResult in project aliyun-oss-java-sdk by aliyun.
the class CopyObjectTest method testCopyObjectWithSpecialChars.
@Ignore
public void testCopyObjectWithSpecialChars() {
final String sourceBucket = "copy-existing-object-source-bucket";
final String targetBucket = "copy-existing-object-target-bucket";
final String sourceKey = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?/.zip";
final String targetKey = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?/-2.zip";
final String userMetaKey0 = "user";
final String userMetaValue0 = "阿里人";
// TODO: With chinese characters will be failed.
final String userMetaKey1 = "tag";
final String userMetaValue1 = "标签1";
final String contentType = "application/txt";
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
// Set source object different with target object and copy source bucket orignal metadata(default behavior).
byte[] content = { 'A', 'l', 'i', 'y', 'u', 'n' };
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(content.length);
metadata.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
metadata.addUserMetadata(userMetaKey0, userMetaValue0);
PutObjectResult putObjectResult = ossClient.putObject(sourceBucket, sourceKey, new ByteArrayInputStream(content), metadata);
CopyObjectResult copyObjectResult = ossClient.copyObject(sourceBucket, sourceKey, targetBucket, targetKey);
String sourceETag = putObjectResult.getETag();
String targetETag = copyObjectResult.getETag();
Assert.assertEquals(sourceETag, targetETag);
OSSObject ossObject = ossClient.getObject(targetBucket, targetKey);
ObjectMetadata newObjectMetadata = ossObject.getObjectMetadata();
Assert.assertEquals(DEFAULT_OBJECT_CONTENT_TYPE, newObjectMetadata.getContentType());
Assert.assertEquals(userMetaValue0, newObjectMetadata.getUserMetadata().get(userMetaKey0));
// Set source object same as target object and replace source bucket orignal metadata.
final String sourceBucketAsTarget = sourceBucket;
final String sourceKeyAsTarget = sourceKey;
newObjectMetadata = new ObjectMetadata();
newObjectMetadata.setContentLength(content.length);
newObjectMetadata.setContentType(contentType);
newObjectMetadata.addUserMetadata(userMetaKey1, userMetaValue1);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucket, sourceKey, sourceBucketAsTarget, sourceKeyAsTarget);
copyObjectRequest.setNewObjectMetadata(newObjectMetadata);
copyObjectResult = ossClient.copyObject(copyObjectRequest);
Assert.assertEquals(sourceETag, copyObjectResult.getETag());
ossObject = ossClient.getObject(sourceBucketAsTarget, sourceKeyAsTarget);
newObjectMetadata = ossObject.getObjectMetadata();
Assert.assertEquals(contentType, newObjectMetadata.getContentType());
Assert.assertEquals(userMetaValue1, newObjectMetadata.getUserMetadata().get(userMetaKey1));
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.CopyObjectResult in project aliyun-oss-java-sdk by aliyun.
the class ResponseParsers method parseCopyObjectResult.
/**
* Unmarshall copy object response body to corresponding result.
*/
public static CopyObjectResult parseCopyObjectResult(InputStream responseBody) throws ResponseParseException {
try {
Element root = getXmlRootElement(responseBody);
CopyObjectResult result = new CopyObjectResult();
result.setLastModified(DateUtil.parseIso8601Date(root.getChildText("LastModified")));
result.setEtag(trimQuotes(root.getChildText("ETag")));
return result;
} catch (Exception e) {
throw new ResponseParseException(e.getMessage(), e);
}
}
Aggregations