Search in sources :

Example 6 with CopyObjectResult

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);
    }
}
Also used : CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with CopyObjectResult

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);
    }
}
Also used : CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) Ignore(org.junit.Ignore)

Example 8 with CopyObjectResult

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);
    }
}
Also used : CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Aggregations

CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)8 PutObjectResult (com.aliyun.oss.model.PutObjectResult)5 OSSException (com.aliyun.oss.OSSException)4 CopyObjectRequest (com.aliyun.oss.model.CopyObjectRequest)4 OSSObject (com.aliyun.oss.model.OSSObject)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)2 Ignore (org.junit.Ignore)2 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)1 AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)1 AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)1 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)1 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)1 DeleteObjectsRequest (com.aliyun.oss.model.DeleteObjectsRequest)1 DeleteObjectsResult (com.aliyun.oss.model.DeleteObjectsResult)1 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)1 InitiateMultipartUploadRequest (com.aliyun.oss.model.InitiateMultipartUploadRequest)1