Search in sources :

Example 21 with UploadPartRequest

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

the class CallbackTest method testMultipartUploadCallbackBodyType.

/**
 * callback body type,type is json
 */
@Test
public void testMultipartUploadCallbackBodyType() {
    String key = "multipart-upload-callback-body-type";
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        InputStream instream = genFixedLengthInputStream(instreamLength);
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(instreamLength);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
        callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
        callback.setCalbackBodyType(CalbackBodyType.JSON);
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        completeMultipartUploadRequest.setCallback(callback);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = completeMultipartUploadResult.getCallbackResponseBody().read(buffer);
        completeMultipartUploadResult.getCallbackResponseBody().close();
        Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
        OSSObject obj = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, obj.getKey());
        Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
        obj.close();
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) Callback(com.aliyun.oss.model.Callback) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 22 with UploadPartRequest

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

the class OSSClientRequestTest method testUploadPartRequest.

@SuppressWarnings("serial")
@Test
public void testUploadPartRequest() throws Exception {
    String uploadId = "upload123";
    final UploadPartRequest request = new UploadPartRequest();
    request.setBucketName(bucketName);
    request.setKey(objectKey);
    request.setUploadId(uploadId);
    request.setPartNumber(1);
    byte[] inputBytes = uploadId.getBytes(OSSConstants.DEFAULT_CHARSET_NAME);
    ByteArrayInputStream input = new ByteArrayInputStream(inputBytes);
    request.setInputStream(input);
    request.setPartSize(inputBytes.length);
    TestAction test1 = new TestAction() {

        public void run() throws Exception {
            multipartOp.uploadPart(request);
        }
    };
    executeTest(test1, HttpMethod.PUT, bucketName + "." + endpoint.getHost(), objectKey + "?partNumber=" + request.getPartNumber() + "&uploadId=" + uploadId, new HashMap<String, String>() {

        {
            put("Content-Length", Long.toString(request.getPartSize()));
        }
    });
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) Test(org.junit.Test)

Aggregations

UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)22 UploadPartResult (com.aliyun.oss.model.UploadPartResult)20 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)20 OSSException (com.aliyun.oss.OSSException)18 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)18 PartETag (com.aliyun.oss.model.PartETag)18 InputStream (java.io.InputStream)18 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)17 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)17 OSSObject (com.aliyun.oss.model.OSSObject)14 FileInputStream (java.io.FileInputStream)13 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)6 Callback (com.aliyun.oss.model.Callback)6 InitiateMultipartUploadRequest (com.aliyun.oss.model.InitiateMultipartUploadRequest)5 InitiateMultipartUploadResult (com.aliyun.oss.model.InitiateMultipartUploadResult)5 ListMultipartUploadsRequest (com.aliyun.oss.model.ListMultipartUploadsRequest)4 ListPartsRequest (com.aliyun.oss.model.ListPartsRequest)4 MultipartUpload (com.aliyun.oss.model.MultipartUpload)4 MultipartUploadListing (com.aliyun.oss.model.MultipartUploadListing)4