Search in sources :

Example 6 with PutObjectResult

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

the class CRCChecksumTest method testGetObjectCRC.

@Test
public void testGetObjectCRC() {
    String key = "get-object-crc";
    try {
        InputStream inputStream = TestUtils.genFixedLengthInputStream(1024 * 100);
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, inputStream);
        checkCRC(putObjectResult);
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        Assert.assertNull(ossObject.getClientCRC());
        Assert.assertNotNull(ossObject.getServerCRC());
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        InputStream content = ossObject.getObjectContent();
        while (content.read() != -1) {
        }
        ossObject.setClientCRC(IOUtils.getCRCValue(content));
        checkCRC(ossObject);
        content.close();
        // 范围CRC上不支持
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        getObjectRequest.setRange(100, 10000);
        OSSObject ossRangeObject = ossClient.getObject(getObjectRequest);
        Assert.assertNull(ossRangeObject.getClientCRC());
        Assert.assertNotNull(ossRangeObject.getServerCRC());
        Assert.assertEquals(ossObject.getServerCRC(), ossRangeObject.getServerCRC());
        Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) InconsistentException(com.aliyun.oss.InconsistentException) Test(org.junit.Test)

Example 7 with PutObjectResult

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

the class CRCChecksumTest method testPutObjectCRC.

@Test
public void testPutObjectCRC() {
    String key = "put-object-crc";
    String content = "Hello OSS, Hi OSS, OSS OK.";
    try {
        // 测试字符串上传
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        checkCRC(putObjectResult);
        ossClient.deleteObject(bucketName, key);
        // 测试文件上传
        File file = createSampleFile(key, 1024 * 500);
        putObjectResult = ossClient.putObject(bucketName, key, file);
        checkCRC(putObjectResult);
        ossClient.deleteObject(bucketName, key);
        file.delete();
        // 测试文件流上传
        file = createSampleFile(key, 1024 * 1000);
        putObjectResult = ossClient.putObject(bucketName, key, new FileInputStream(file));
        checkCRC(putObjectResult);
        ossClient.deleteObject(bucketName, key);
        file.delete();
        // 测试网络流上传
        InputStream inputStream = new URL("https://www.aliyun.com/product/oss").openStream();
        putObjectResult = ossClient.putObject(bucketName, key, inputStream);
        checkCRC(putObjectResult);
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) File(java.io.File) FileInputStream(java.io.FileInputStream) URL(java.net.URL) InconsistentException(com.aliyun.oss.InconsistentException) Test(org.junit.Test)

Example 8 with PutObjectResult

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

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

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

the class GetObjectTest method testUnormalGetObject.

@Test
public void testUnormalGetObject() throws Exception {
    final Date beforeModifiedTime = new Date();
    Thread.sleep(1000);
    // Try to get object under nonexistent bucket
    final String key = "unormal-get-object";
    final String nonexistentBucket = "nonexistent-bukcet";
    try {
        ossClient.getObject(nonexistentBucket, key);
        Assert.fail("Get object should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, ex.getErrorCode());
        Assert.assertTrue(ex.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
    }
    // Try to get nonexistent object
    final String nonexistentKey = "nonexistent-object";
    try {
        ossClient.getObject(bucketName, nonexistentKey);
        Assert.fail("Get object should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, ex.getErrorCode());
        Assert.assertTrue(ex.getMessage().startsWith(NO_SUCH_KEY_ERR));
    }
    String eTag = null;
    try {
        PutObjectResult result = ossClient.putObject(bucketName, key, genFixedLengthInputStream(1024), null);
        eTag = result.getETag();
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    Thread.sleep(2000);
    // Matching ETag Constraints
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    List<String> matchingETagConstraints = new ArrayList<String>();
    OSSObject o = null;
    matchingETagConstraints.add(eTag);
    getObjectRequest.setMatchingETagConstraints(matchingETagConstraints);
    try {
        o = ossClient.getObject(getObjectRequest);
        Assert.assertEquals(eTag, o.getObjectMetadata().getETag());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        getObjectRequest.setMatchingETagConstraints(null);
    }
    matchingETagConstraints.clear();
    matchingETagConstraints.add("nonmatching-etag");
    getObjectRequest.setMatchingETagConstraints(matchingETagConstraints);
    try {
        o = ossClient.getObject(getObjectRequest);
        Assert.fail("Get object should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.PRECONDITION_FAILED, e.getErrorCode());
    } finally {
        getObjectRequest.setMatchingETagConstraints(null);
    }
    // Non-Matching ETag Constraints
    List<String> nonmatchingETagConstraints = new ArrayList<String>();
    nonmatchingETagConstraints.add("nonmatching-etag");
    getObjectRequest.setNonmatchingETagConstraints(nonmatchingETagConstraints);
    try {
        o = ossClient.getObject(getObjectRequest);
        Assert.assertEquals(eTag, o.getObjectMetadata().getETag());
    } catch (OSSException e) {
        Assert.fail(e.getMessage());
    } finally {
        getObjectRequest.setNonmatchingETagConstraints(null);
    }
    nonmatchingETagConstraints.clear();
    nonmatchingETagConstraints.add(eTag);
    getObjectRequest.setNonmatchingETagConstraints(nonmatchingETagConstraints);
    try {
        o = ossClient.getObject(getObjectRequest);
        Assert.fail("Get object should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NOT_MODIFIED, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(NOT_MODIFIED_ERR));
    } finally {
        getObjectRequest.setNonmatchingETagConstraints(null);
    }
    // Unmodified Since Constraint
    Date unmodifiedSinceConstraint = new Date();
    getObjectRequest.setUnmodifiedSinceConstraint(unmodifiedSinceConstraint);
    try {
        o = ossClient.getObject(getObjectRequest);
        Assert.assertEquals(eTag, o.getObjectMetadata().getETag());
    } catch (OSSException e) {
        Assert.fail(e.getMessage());
    } finally {
        getObjectRequest.setUnmodifiedSinceConstraint(null);
    }
    unmodifiedSinceConstraint = beforeModifiedTime;
    getObjectRequest.setUnmodifiedSinceConstraint(unmodifiedSinceConstraint);
    try {
        o = ossClient.getObject(getObjectRequest);
        Assert.fail("Get object should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.PRECONDITION_FAILED, e.getErrorCode());
    } finally {
        getObjectRequest.setUnmodifiedSinceConstraint(null);
    }
    // Modified Since Constraint
    Date modifiedSinceConstraint = beforeModifiedTime;
    getObjectRequest.setModifiedSinceConstraint(modifiedSinceConstraint);
    try {
        o = ossClient.getObject(getObjectRequest);
        Assert.assertEquals(eTag, o.getObjectMetadata().getETag());
    } catch (OSSException e) {
        Assert.fail(e.getMessage());
    } finally {
        getObjectRequest.setModifiedSinceConstraint(null);
    }
    modifiedSinceConstraint = new Date();
    getObjectRequest.setModifiedSinceConstraint(modifiedSinceConstraint);
    try {
        o = ossClient.getObject(getObjectRequest);
        Assert.fail("Get object should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NOT_MODIFIED, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(NOT_MODIFIED_ERR));
    } finally {
        getObjectRequest.setModifiedSinceConstraint(null);
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) Date(java.util.Date) OSSException(com.aliyun.oss.OSSException) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

PutObjectResult (com.aliyun.oss.model.PutObjectResult)59 Test (org.junit.Test)31 OSSException (com.aliyun.oss.OSSException)30 InputStream (java.io.InputStream)24 OSSObject (com.aliyun.oss.model.OSSObject)20 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)19 File (java.io.File)15 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)13 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 FileInputStream (java.io.FileInputStream)9 Callback (com.aliyun.oss.model.Callback)8 IOException (java.io.IOException)8 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)7 Date (java.util.Date)7 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)6 Ignore (org.junit.Ignore)6 OSS (com.aliyun.oss.OSS)5 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)5