Search in sources :

Example 1 with CopyObjectRequest

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

the class OSSClientRequestTest method testCopyObjectRequest.

@SuppressWarnings("serial")
@Test
public void testCopyObjectRequest() {
    final String sourceBucketName = "src_bucket";
    final String sourceKey = "src_key";
    final CopyObjectRequest request = new CopyObjectRequest(sourceBucketName, sourceKey, bucketName, objectKey);
    TestAction test1 = new TestAction() {

        public void run() throws Exception {
            objectOp.copyObject(request);
        }
    };
    executeTest(test1, HttpMethod.PUT, bucketName + "." + endpoint.getHost(), objectKey, new HashMap<String, String>() {

        {
            put("x-oss-copy-source", "/" + sourceBucketName + "/" + sourceKey);
        }
    }, null, 0);
    final List<String> matchingETags = new LinkedList<String>();
    matchingETags.add("matching");
    matchingETags.add("m2");
    request.setMatchingETagConstraints(matchingETags);
    final List<String> unmatchingETags = new LinkedList<String>();
    unmatchingETags.add("unmatching");
    unmatchingETags.add("u2");
    request.setNonmatchingETagConstraints(unmatchingETags);
    request.setModifiedSinceConstraint(new Date());
    request.setUnmodifiedSinceConstraint(new Date());
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentType("text/plain");
    metadata.setContentEncoding(OSSConstants.DEFAULT_CHARSET_NAME);
    metadata.setCacheControl("no-cache");
    metadata.setServerSideEncryption(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    metadata.setUserMetadata(new HashMap<String, String>() {

        {
            put("my", "my");
        }
    });
    request.setNewObjectMetadata(metadata);
    final Map<String, String> expectedHeaders = new HashMap<String, String>() {

        {
            put("x-oss-copy-source-if-match", matchingETags.get(0) + ", " + matchingETags.get(1));
            put("x-oss-copy-source-if-none-match", unmatchingETags.get(0) + ", " + unmatchingETags.get(1));
            put("x-oss-copy-source-if-modified-since", DateUtil.formatRfc822Date(request.getModifiedSinceConstraint()));
            put("x-oss-copy-source-if-unmodified-since", DateUtil.formatRfc822Date(request.getUnmodifiedSinceConstraint()));
            put("x-oss-metadata-directive", "REPLACE");
            put("Content-Type", metadata.getContentType());
            put("Content-Encoding", metadata.getContentEncoding());
            put("Cache-Control", metadata.getCacheControl());
            put("x-oss-server-side-encryption", metadata.getServerSideEncryption());
            put("x-oss-meta-my", "my");
        }
    };
    TestAction test2 = new TestAction() {

        public void run() throws Exception {
            objectOp.copyObject(request);
        }
    };
    executeTest(test2, HttpMethod.PUT, bucketName + "." + endpoint.getHost(), objectKey, expectedHeaders, null, 0);
}
Also used : CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) HashMap(java.util.HashMap) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) LinkedList(java.util.LinkedList) Date(java.util.Date) Test(org.junit.Test)

Example 2 with CopyObjectRequest

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

the class CopyObjectTest method testCopyExistingObject.

@Test
public void testCopyExistingObject() {
    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);
        // 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);
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        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));
        Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
        // 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));
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(copyObjectResult.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) 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) Test(org.junit.Test)

Example 3 with CopyObjectRequest

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

the class CopyObjectTest method testCopyObjectWithMiscConstraints.

@Ignore
public void testCopyObjectWithMiscConstraints() throws Exception {
    final String sourceBucket = "copy-object-with-misc-constraints-source-bucket";
    final String targetBucket = "copy-object-with-misc-constraints-target-bucket";
    final String sourceKey = "copy-object-with-misc-constraints-source-key";
    final String targetKey = "copy-object-with-misc-constraints-target-key";
    try {
        ossClient.createBucket(sourceBucket);
        ossClient.createBucket(targetBucket);
        final Date beforeModifiedTime = new Date();
        Thread.sleep(1000);
        String eTag = null;
        try {
            PutObjectResult result = ossClient.putObject(sourceBucket, sourceKey, TestUtils.genFixedLengthInputStream(1024), null);
            eTag = result.getETag();
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        // Matching ETag Constraints
        List<String> matchingETagConstraints = new ArrayList<String>();
        matchingETagConstraints.add(eTag);
        CopyObjectRequest request = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
        request.setMatchingETagConstraints(matchingETagConstraints);
        CopyObjectResult result = null;
        try {
            result = ossClient.copyObject(request);
            Assert.assertEquals(eTag, result.getETag());
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        } finally {
            request.clearMatchingETagConstraints();
        }
        matchingETagConstraints.clear();
        matchingETagConstraints.add("nonmatching-etag");
        request.setMatchingETagConstraints(matchingETagConstraints);
        try {
            result = ossClient.copyObject(request);
            Assert.fail("Copy object should not be successful.");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.PRECONDITION_FAILED, e.getErrorCode());
        // Assert.assertTrue(e.getMessage().startsWith(PRECONDITION_FAILED_ERR));
        } finally {
            request.clearMatchingETagConstraints();
        }
        // Non-Matching ETag Constraints
        List<String> nonmatchingETagConstraints = new ArrayList<String>();
        nonmatchingETagConstraints.add("nonmatching-etag");
        request.setNonmatchingETagConstraints(nonmatchingETagConstraints);
        try {
            result = ossClient.copyObject(request);
            Assert.assertEquals(eTag, result.getETag());
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        } finally {
            request.clearNonmatchingETagConstraints();
        }
        nonmatchingETagConstraints.clear();
        nonmatchingETagConstraints.add(eTag);
        request.setNonmatchingETagConstraints(nonmatchingETagConstraints);
        try {
            result = ossClient.copyObject(request);
            Assert.fail("Copy object should not be successful.");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NOT_MODIFIED, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(NOT_MODIFIED_ERR));
        } finally {
            request.clearNonmatchingETagConstraints();
        }
        // Unmodified Since Constraint
        Date unmodifiedSinceConstraint = new Date();
        request.setUnmodifiedSinceConstraint(unmodifiedSinceConstraint);
        try {
            result = ossClient.copyObject(request);
            Assert.assertEquals(eTag, result.getETag());
        } catch (OSSException e) {
            Assert.fail(e.getMessage());
        } finally {
            request.setUnmodifiedSinceConstraint(null);
        }
        unmodifiedSinceConstraint = beforeModifiedTime;
        request.setUnmodifiedSinceConstraint(unmodifiedSinceConstraint);
        try {
            result = ossClient.copyObject(request);
            Assert.fail("Copy object should not be successful.");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.PRECONDITION_FAILED, e.getErrorCode());
        // Assert.assertTrue(e.getMessage().startsWith(PRECONDITION_FAILED_ERR));
        } finally {
            request.setUnmodifiedSinceConstraint(null);
        }
        // Modified Since Constraint
        Date modifiedSinceConstraint = beforeModifiedTime;
        request.setModifiedSinceConstraint(modifiedSinceConstraint);
        try {
            result = ossClient.copyObject(request);
            Assert.assertEquals(eTag, result.getETag());
        } catch (OSSException e) {
            Assert.fail(e.getMessage());
        } finally {
            request.setModifiedSinceConstraint(null);
        }
        modifiedSinceConstraint = new Date();
        request.setModifiedSinceConstraint(modifiedSinceConstraint);
        try {
            result = ossClient.copyObject(request);
            Assert.fail("Copy object should not be successful.");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NOT_MODIFIED, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(NOT_MODIFIED_ERR));
        } finally {
            request.setModifiedSinceConstraint(null);
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        deleteBucketWithObjects(ossClient, sourceBucket);
        deleteBucketWithObjects(ossClient, targetBucket);
    }
}
Also used : CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) PutObjectResult(com.aliyun.oss.model.PutObjectResult) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) Date(java.util.Date) OSSException(com.aliyun.oss.OSSException) Ignore(org.junit.Ignore)

Example 4 with CopyObjectRequest

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

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

the class CopyObjectTest method testCopyObjectWithInvalidEncryptionAlgo.

@Ignore
public void testCopyObjectWithInvalidEncryptionAlgo() {
    final String sourceBucket = "copy-object-with-invalid-encryption-algo-source-bucket";
    final String targetBucket = "copy-object-with-invalid-encryption-algo-target-bucket";
    final String sourceKey = "copy-object-with-invalid-encryption-algo-source-key";
    final String targetKey = "copy-object-with-invalid-encryption-algo-target-key";
    try {
        ossClient.createBucket(sourceBucket);
        ossClient.createBucket(targetBucket);
        final String invalidEncryptionAlgo = "Invalid-Encryption-Algo";
        try {
            CopyObjectRequest request = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
            request.setServerSideEncryption(invalidEncryptionAlgo);
            ossClient.copyObject(request);
            Assert.fail("Copy object should not be successful");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.INVALID_ENCRYPTION_ALGORITHM_ERROR, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(INVALID_ENCRYPTION_ALGO_ERR));
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        deleteBucketWithObjects(ossClient, sourceBucket);
        deleteBucketWithObjects(ossClient, targetBucket);
    }
}
Also used : CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) OSSException(com.aliyun.oss.OSSException) OSSException(com.aliyun.oss.OSSException) Ignore(org.junit.Ignore)

Aggregations

CopyObjectRequest (com.aliyun.oss.model.CopyObjectRequest)8 OSSException (com.aliyun.oss.OSSException)6 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 PutObjectResult (com.aliyun.oss.model.PutObjectResult)4 Test (org.junit.Test)4 OSSObject (com.aliyun.oss.model.OSSObject)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Ignore (org.junit.Ignore)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 OSS (com.aliyun.oss.OSS)1 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)1 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)1 DefaultServiceClient (com.aliyun.oss.common.comm.DefaultServiceClient)1 OSSObjectOperation (com.aliyun.oss.internal.OSSObjectOperation)1 ListObjectsRequest (com.aliyun.oss.model.ListObjectsRequest)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1