Search in sources :

Example 1 with AppendObjectRequest

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

the class OSSObjectOperation method populateWriteObjectParams.

private static void populateWriteObjectParams(WriteMode mode, PutObjectRequest originalRequest, Map<String, String> params) {
    if (mode == WriteMode.OVERWRITE) {
        return;
    }
    assert (originalRequest instanceof AppendObjectRequest);
    params.put(RequestParameters.SUBRESOURCE_APPEND, null);
    AppendObjectRequest appendObjectRequest = (AppendObjectRequest) originalRequest;
    if (appendObjectRequest.getPosition() != null) {
        params.put(RequestParameters.POSITION, String.valueOf(appendObjectRequest.getPosition()));
    }
}
Also used : AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest)

Example 2 with AppendObjectRequest

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

the class AppendObjectSample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        /*
             * Append an object from specfied input stream, keep in mind that
             * position should be set to zero at first time.
             */
        String content = "Thank you for using Aliyun Object Storage Service";
        InputStream instream = new ByteArrayInputStream(content.getBytes());
        Long firstPosition = 0L;
        System.out.println("Begin to append object at position(" + firstPosition + ")");
        AppendObjectResult appendObjectResult = client.appendObject(new AppendObjectRequest(bucketName, key, instream).withPosition(0L));
        System.out.println("\tNext position=" + appendObjectResult.getNextPosition() + ", CRC64=" + appendObjectResult.getObjectCRC() + "\n");
        /*
             * Continue to append the object from specfied file descriptor at last position
             */
        Long nextPosition = appendObjectResult.getNextPosition();
        System.out.println("Continue to append object at last position(" + nextPosition + "):");
        appendObjectResult = client.appendObject(new AppendObjectRequest(bucketName, key, createTempFile()).withPosition(nextPosition));
        System.out.println("\tNext position=" + appendObjectResult.getNextPosition() + ", CRC64=" + appendObjectResult.getObjectCRC());
        /*
             * View object type of the appendable object
             */
        OSSObject object = client.getObject(bucketName, key);
        System.out.println("\tObject type=" + object.getObjectMetadata().getObjectType() + "\n");
        // Do not forget to close object input stream if not use it any more
        object.getObjectContent().close();
        /*
             * Delete the appendable object
             */
        System.out.println("Deleting an appendable object");
        client.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());
    } finally {
        /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
        client.shutdown();
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult)

Example 3 with AppendObjectRequest

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

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

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

the class CRCChecksumTest method testAppendObjectCRC.

@Test
public void testAppendObjectCRC() {
    String key = "append-object-crc";
    String content = "Hello OSS, Hi OSS.";
    try {
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes())).withPosition(0L);
        // 第一次追加
        appendObjectRequest.setInitCRC(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        checkCRC(appendObjectResult);
        // 第二次追加
        appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
        appendObjectRequest.setInitCRC(appendObjectResult.getClientCRC());
        appendObjectResult = ossClient.appendObject(appendObjectRequest);
        checkCRC(appendObjectResult);
        // 第三次追加
        appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
        appendObjectRequest.setInitCRC(appendObjectResult.getClientCRC());
        appendObjectResult = ossClient.appendObject(appendObjectRequest);
        checkCRC(appendObjectResult);
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) InconsistentException(com.aliyun.oss.InconsistentException) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) Test(org.junit.Test)

Aggregations

AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)11 Test (org.junit.Test)8 OSSException (com.aliyun.oss.OSSException)7 AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)7 OSSObject (com.aliyun.oss.model.OSSObject)7 InputStream (java.io.InputStream)7 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InconsistentException (com.aliyun.oss.InconsistentException)3 File (java.io.File)3 ClientException (com.aliyun.oss.ClientException)2 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)2 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)2 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)1 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)1 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)1 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)1 DeleteObjectsRequest (com.aliyun.oss.model.DeleteObjectsRequest)1