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();
}
}
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());
}
}
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());
}
}
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());
}
}
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());
}
}
Aggregations