Search in sources :

Example 1 with InconsistentException

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

the class CRCSample method main.

public static void main(String[] args) throws IOException {
    String content = "Hello OSS, Hi OSS, OSS OK.";
    // CRC check is enabled by default for upload or download. To turn it off, use the commented code below.
    // ClientConfiguration config = new ClientConfiguration();
    // config.setCrcCheckEnabled(false);
    // OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        // putObject/uploadPart/uploadFile are automatically enabled with CRC.
        // However, appendObject needs to call AppendObjectRequest.setInitCRC to enable CRC.
        ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.deleteObject(bucketName, key);
        // First data appending
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes())).withPosition(0L);
        // because it's first append, the previous data is empty and thus CRC is 0.
        appendObjectRequest.setInitCRC(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        // Second data appending
        appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
        // the initCRC is the first's CRC.
        appendObjectRequest.setInitCRC(appendObjectResult.getClientCRC());
        appendObjectResult = ossClient.appendObject(appendObjectRequest);
        ossClient.deleteObject(bucketName, key);
        // Upload with checkpoint, it supports CRC as well.
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        // Sets the upload file with a local file.
        uploadFileRequest.setUploadFile(uploadFile);
        // Sets the concurrent task number to 5 (default is 1).
        uploadFileRequest.setTaskNum(5);
        // Sets the part size to 1MB (default is 100K)
        uploadFileRequest.setPartSize(1024 * 1024 * 1);
        // Enable checkpoint (by default is off for checkpoint enabled upload)
        uploadFileRequest.setEnableCheckpoint(true);
        ossClient.uploadFile(uploadFileRequest);
        // Download with CRC. Note that range download does not support CRC.
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        Assert.assertNull(ossObject.getClientCRC());
        Assert.assertNotNull(ossObject.getServerCRC());
        InputStream stream = ossObject.getObjectContent();
        while (stream.read() != -1) {
        }
        stream.close();
        // Check if CRC is consistent.
        OSSUtils.checkChecksum(IOUtils.getCRCValue(stream), ossObject.getServerCRC(), ossObject.getRequestId());
        ossClient.deleteObject(bucketName, key);
    } catch (OSSException oe) {
        System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
        System.out.println("Error Message: " + oe.getErrorCode());
        System.out.println("Error Code:       " + oe.getErrorCode());
        System.out.println("Request ID:      " + oe.getRequestId());
        System.out.println("Host ID:           " + oe.getHostId());
    } catch (ClientException ce) {
        System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
        System.out.println("Error Message: " + ce.getMessage());
    } catch (InconsistentException ie) {
        System.out.println("Caught an OSSException");
        System.out.println("Request ID:      " + ie.getRequestId());
    } catch (Throwable e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OSSException(com.aliyun.oss.OSSException) InconsistentException(com.aliyun.oss.InconsistentException) OSS(com.aliyun.oss.OSS) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) ClientException(com.aliyun.oss.ClientException) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 2 with InconsistentException

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

the class CRCChecksumTest method testAppendObjectCRCNegative.

@Test
public void testAppendObjectCRCNegative() {
    String key = "append-object-crc-neg";
    String content = "Hello OSS, Hi OSS.";
    try {
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes())).withPosition(0L);
        appendObjectRequest.setInitCRC(1L);
        ossClient.appendObject(appendObjectRequest);
        Assert.fail("Append object should not be successful.");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof InconsistentException);
        ossClient.deleteObject(bucketName, key);
        System.out.println(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) InconsistentException(com.aliyun.oss.InconsistentException) InconsistentException(com.aliyun.oss.InconsistentException) Test(org.junit.Test)

Aggregations

InconsistentException (com.aliyun.oss.InconsistentException)2 AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 OSSException (com.aliyun.oss.OSSException)1 AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)1 OSSObject (com.aliyun.oss.model.OSSObject)1 UploadFileRequest (com.aliyun.oss.model.UploadFileRequest)1 InputStream (java.io.InputStream)1 Test (org.junit.Test)1