Search in sources :

Example 16 with UploadPartRequest

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

the class ChunkedEncodingTest method testUploadPartChunked.

@Ignore
public void testUploadPartChunked() {
    try {
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = client.initiateMultipartUpload(initiateMultipartUploadRequest);
        String uploadId = initiateMultipartUploadResult.getUploadId();
        File file = new File(filePath);
        final int partSize = 5 * 1024 * 1024;
        int fileSize = (int) file.length();
        final int partCount = (file.length() % partSize != 0) ? (fileSize / partSize + 1) : (fileSize / partSize);
        List<PartETag> partETags = new ArrayList<PartETag>();
        for (int i = 0; i < partCount; i++) {
            InputStream fin = new BufferedInputStream(new FileInputStream(file));
            fin.skip(i * partSize);
            int size = (i + 1 == partCount) ? (fileSize - i * partSize) : partSize;
            UploadPartRequest req = new UploadPartRequest();
            req.setBucketName(bucketName);
            req.setKey(key);
            req.setPartNumber(i + 1);
            req.setPartSize(size);
            req.setUploadId(uploadId);
            req.setInputStream(fin);
            req.setUseChunkEncoding(true);
            UploadPartResult result = client.uploadPart(req);
            partETags.add(result.getPartETag());
        }
        String expectedETag = calcMultipartETag(partETags);
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = client.completeMultipartUpload(completeMultipartUploadRequest);
        String actualETag = completeMultipartUploadResult.getETag();
        Assert.assertEquals(expectedETag, actualETag);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) FileInputStream(java.io.FileInputStream) UploadPartResult(com.aliyun.oss.model.UploadPartResult) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Ignore(org.junit.Ignore)

Example 17 with UploadPartRequest

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

the class CallbackTest method testMultipartUploadCallbackDefault.

/**
 * Tests default values with specified url and body.
 */
@Test
public void testMultipartUploadCallbackDefault() {
    String key = "multipart-upload-callback-default";
    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.setCallbackBody("upload-object-callback");
        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());
    } 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 18 with UploadPartRequest

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

the class CallbackTest method testMultipartUploadCallbackVar.

/**
 * callback var
 */
@Test
public void testMultipartUploadCallbackVar() {
    String key = "multipart-upload-callback-var";
    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);
        callback.addCallbackVar("x:var1", "value1");
        callback.addCallbackVar("x:var2", "value2");
        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());
    } 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 19 with UploadPartRequest

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

the class CallbackTest method testMultipartUploadCallbackURLChar.

/**
 * callback body/var has special characters, type is url
 */
@Test
public void testMultipartUploadCallbackURLChar() {
    String key = "multipart-upload-callback-url-char";
    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("春水碧于天,画船听雨眠。垆边人似月,皓腕凝霜雪。");
        callback.setCalbackBodyType(CalbackBodyType.JSON);
        callback.addCallbackVar("x:键值1", "值1:凌波不过横塘路,但目送,芳尘去。");
        callback.addCallbackVar("x:键值2", "值2:长记曾携手处,千树压、西湖寒碧。");
        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());
    } 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 20 with UploadPartRequest

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

the class CallbackTest method testMultipartUploadCallbackJsonChar.

/**
 * callback body/var has special characters, type is json.
 */
@Test
public void testMultipartUploadCallbackJsonChar() {
    String key = "multipart-upload-callback-json-char";
    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("{\\\"上片\\\":\\\"夏日好,月色白如雪。\\\" ,\\\"下片\\\":\\\"东山照欢会,西山照离别。 夏日好,花月有清阴。\\\"}");
        callback.setCalbackBodyType(CalbackBodyType.JSON);
        callback.addCallbackVar("x:键值1", "值1:凌波不过横塘路,但目送,芳尘去。");
        callback.addCallbackVar("x:键值2", "值2:长记曾携手处,千树压、西湖寒碧。");
        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());
    } 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)

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