Search in sources :

Example 6 with PutObjectRequest

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

the class GetProgressSample method main.

public static void main(String[] args) {
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    String accessKeyId = "<accessKeyId>";
    String accessKeySecret = "<accessKeySecret>";
    String bucketName = "<bucketName>";
    String key = "object-get-progress-sample";
    OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        File fh = createSampleFile();
        // 带进度条的上传
        client.putObject(new PutObjectRequest(bucketName, key, fh).<PutObjectRequest>withProgressListener(new PutObjectProgressListener()));
        // 带进度条的下载
        client.getObject(new GetObjectRequest(bucketName, key).<GetObjectRequest>withProgressListener(new GetObjectProgressListener()), fh);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : File(java.io.File) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) IOException(java.io.IOException)

Example 7 with PutObjectRequest

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

the class ProgressBarTest method testSimpleObjectProgressBar.

@Test
public void testSimpleObjectProgressBar() throws Exception {
    final String key = "put-object-progress-bar";
    final int instreamLength = 64 * 1024;
    final String filePath = TestUtils.buildFilePath();
    try {
        File fileToUpload = new File(genFixedLengthFile(instreamLength));
        ossClient.putObject(new PutObjectRequest(bucketName, key, fileToUpload).<PutObjectRequest>withProgressListener(new PutObjectProgressListener()));
        ObjectMetadata metadata = ossClient.getObject(new GetObjectRequest(bucketName, key).<GetObjectRequest>withProgressListener(new GetObjectProgressListener()), new File(filePath));
        Assert.assertEquals(instreamLength, metadata.getContentLength());
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with PutObjectRequest

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

the class CallbackTest method testPutObjectCallbackVar.

/**
 * callback body type,type of callback body is json
 */
@Test
public void testPutObjectCallbackVar() throws Exception {
    String key = "put-callback-var";
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        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");
        putObjectRequest.setCallback(callback);
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = putObjectResult.getResponse().getContent().read(buffer);
        putObjectResult.getResponse().getContent().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 : Callback(com.aliyun.oss.model.Callback) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 9 with PutObjectRequest

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

the class CallbackTest method testPutObjectCallbacURLChar.

/**
 * callback body var has special characters, type is json.
 */
@Test
public void testPutObjectCallbacURLChar() throws Exception {
    String key = "put-callback-url-char";
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        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:长记曾携手处,千树压、西湖寒碧。");
        putObjectRequest.setCallback(callback);
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = putObjectResult.getCallbackResponseBody().read(buffer);
        putObjectResult.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 : Callback(com.aliyun.oss.model.Callback) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 10 with PutObjectRequest

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

the class CallbackTest method testPutObjectCallbackBodyType.

/**
 * callback body type,type of callback body is json
 */
@Test
public void testPutObjectCallbackBodyType() throws Exception {
    String key = "put-callback-body-type";
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
        callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
        callback.setCalbackBodyType(CalbackBodyType.JSON);
        putObjectRequest.setCallback(callback);
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = putObjectResult.getCallbackResponseBody().read(buffer);
        putObjectResult.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 : Callback(com.aliyun.oss.model.Callback) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)24 OSSException (com.aliyun.oss.OSSException)18 Test (org.junit.Test)14 OSSObject (com.aliyun.oss.model.OSSObject)13 PutObjectResult (com.aliyun.oss.model.PutObjectResult)9 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)8 Callback (com.aliyun.oss.model.Callback)8 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)8 InputStream (java.io.InputStream)8 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)7 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)7 IOException (java.io.IOException)6 ClientException (com.aliyun.oss.ClientException)5 OSS (com.aliyun.oss.OSS)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)3 ParseException (java.text.ParseException)3 TestUtils.genRandomLengthFile (com.aliyun.oss.integrationtests.TestUtils.genRandomLengthFile)2 TestUtils.removeFile (com.aliyun.oss.integrationtests.TestUtils.removeFile)2