Search in sources :

Example 1 with CopyObjectResult

use of com.aliyun.oss.model.CopyObjectResult in project hadoop by apache.

the class AliyunOSSFileSystemStore method singleCopy.

/**
   * Use single copy to copy an OSS object.
   * (The caller should make sure srcPath is a file and dstPath is valid)
   *
   * @param srcKey source key.
   * @param dstKey destination key.
   * @return true if object is successfully copied.
   */
private boolean singleCopy(String srcKey, String dstKey) {
    CopyObjectResult copyResult = ossClient.copyObject(bucketName, srcKey, bucketName, dstKey);
    LOG.debug(copyResult.getETag());
    return true;
}
Also used : CopyObjectResult(com.aliyun.oss.model.CopyObjectResult)

Example 2 with CopyObjectResult

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

the class OSSResponseParserTest method testCopyObjectResult.

@Test
public void testCopyObjectResult() throws Exception {
    String filename = "copyObject.xml";
    InputStream in = getInputStream(filename);
    CopyObjectResult result = ResponseParsers.parseCopyObjectResult(in);
    Assert.assertEquals("4F62D1D6EF439E057D4BD20F43DC2C84", result.getETag());
    Assert.assertEquals("Wed, 27 Jun 2012 07:28:49 GMT", DateUtil.formatRfc822Date(result.getLastModified()));
}
Also used : InputStream(java.io.InputStream) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) Test(org.junit.Test)

Example 3 with CopyObjectResult

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

the class GetRequestIdTest method testNormalGetRequestId.

@Test
public void testNormalGetRequestId() {
    final String key = "normal-get-request-id";
    final long inputStreamLength = 1024;
    final int requestIdLength = "572BF2F2207FB3397648E9F1".length();
    try {
        // put object
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, genFixedLengthInputStream(inputStreamLength));
        Assert.assertEquals(putObjectResult.getRequestId().length(), requestIdLength);
        // get object
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        ossObject.getObjectContent().close();
        Assert.assertEquals(ossObject.getRequestId().length(), requestIdLength);
        File file = new File("tmp");
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        ObjectMetadata objectMeta = ossClient.getObject(getObjectRequest, file);
        Assert.assertEquals(objectMeta.getRequestId().length(), requestIdLength);
        // delete object
        ossClient.deleteObject(bucketName, key);
        // append object
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, file);
        appendObjectRequest.setPosition(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        Assert.assertEquals(appendObjectResult.getRequestId().length(), requestIdLength);
        // getSimplifiedObjectMeta
        SimplifiedObjectMeta simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(bucketName, key);
        Assert.assertEquals(simplifiedObjectMeta.getRequestId().length(), requestIdLength);
        // getObjectMetadata
        ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, key);
        Assert.assertEquals(objectMetadata.getRequestId().length(), requestIdLength);
        // delete objects
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
        ArrayList<String> keys = new ArrayList<String>();
        keys.add(key);
        deleteObjectsRequest.setKeys(keys);
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
        Assert.assertEquals(deleteObjectsResult.getRequestId().length(), requestIdLength);
        // initiate multipart upload
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
        Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), requestIdLength);
        // upload part
        UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), 1, new FileInputStream(file), inputStreamLength);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        Assert.assertEquals(uploadPartResult.getRequestId().length(), requestIdLength);
        // complete multipart upload
        List<PartETag> partETags = new ArrayList<PartETag>();
        partETags.add(new PartETag(1, uploadPartResult.getETag()));
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), requestIdLength);
        // copy object
        CopyObjectResult CopyObjectResult = ossClient.copyObject(bucketName, key, bucketName, key);
        Assert.assertEquals(CopyObjectResult.getRequestId().length(), requestIdLength);
        // initiate multipart copy
        InitiateMultipartUploadRequest initiateMultipartCopyRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartCopyResult = ossClient.initiateMultipartUpload(initiateMultipartCopyRequest);
        Assert.assertEquals(initiateMultipartCopyResult.getRequestId().length(), requestIdLength);
        // upload part copy
        UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucketName, key, bucketName, key, initiateMultipartCopyResult.getUploadId(), 1, 0L, inputStreamLength);
        UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
        Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), requestIdLength);
        // abort multipart upload
        AbortMultipartUploadRequest AbortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, initiateMultipartCopyResult.getUploadId());
        ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
        ossClient.deleteObject(bucketName, key);
        file.delete();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : SimplifiedObjectMeta(com.aliyun.oss.model.SimplifiedObjectMeta) InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) UploadPartResult(com.aliyun.oss.model.UploadPartResult) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) FileInputStream(java.io.FileInputStream) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 4 with CopyObjectResult

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

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

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