Append Object 追加上传
Append Object以追加写的方式上传文件。通过Append Object操作创建的Object类型为Appendable Object,而通过Put Object上传的Object是Normal Object。
- AppendObjectRequest append = new AppendObjectRequest(testBucket, testObject, uploadFilePath);
- ObjectMetadata metadata = new ObjectMetadata();
- metadata.setContentType("application/octet-stream");
- append.setMetadata(metadata);
- // 设置追加位置
- append.setPosition(0);
- append.setProgressCallback(new OSSProgressCallback<AppendObjectRequest>() {
- @Override
- public void onProgress(AppendObjectRequest request, long currentSize, long totalSize) {
- Log.d("AppendObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
- }
- });
- OSSAsyncTask task = oss.asyncAppendObject(append, new OSSCompletedCallback<AppendObjectRequest, AppendObjectResult>() {
- @Override
- public void onSuccess(AppendObjectRequest request, AppendObjectResult result) {
- Log.d("AppendObject", "AppendSuccess");
- Log.d("NextPosition", "" + result.getNextPosition());
- }
- @Override
- public void onFailure(AppendObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
- // 异常处理
- }
- });
用户使用Append方式上传,关键要对Position这个参数进行正确的设置:
当用户创建一个Appendable Object时,追加位置设为0。
当对Appendable Object进行内容追加时,追加位置设为Object当前长度。有两种方式获取该Object长度:通过上传追加后的返回内容获取。- 通过head object获取文件长度。
上传后回调通知
客户端在上传Object时可以指定OSS服务端在处理完上传请求后,通知您的业务服务器,在该服务器确认接收了该回调后将回调的结果返回给客户端。因为加入了回调请求和响应的过程,相比简单上传,使用回调通知机制一般会导致客户端花费更多的等待时间。
具体说明参考:
Callback
代码示例:
- PutObjectRequest put = new PutObjectRequest(testBucket, testObject, uploadFilePath);
- put.setCallbackParam(new HashMap<String, String>() {
- {
- put("callbackUrl", "110.75.82.106/callback");
- put("callbackHost", "oss-cn-hangzhou.aliyuncs.com");
- put("callbackBodyType", "application/json");
- put("callbackBody", "{\"mimeType\":${mimeType},\"size\":${size}}");
- }
- });
- OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
- @Override
- public void onSuccess(PutObjectRequest request, PutObjectResult result) {
- Log.d("PutObject", "UploadSuccess");
- // 只有设置了servercallback,这个值才有数据
- String serverCallbackReturnJson = result.getServerCallbackReturnBody();
- Log.d("servercallback", serverCallbackReturnJson);
- }
- @Override
- public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
- // 异常处理
- }
- });
如果需要支持自定义参数,参考如下设置:
- put.setCallbackParam(new HashMap<String, String>() {
- {
- put("callbackUrl", "http://182.92.192.125/leibin/notify.php");
- put("callbackHost", "oss-cn-hangzhou.aliyuncs.com");
- put("callbackBodyType", "application/json");
- put("callbackBody", "{\"object\":${object},\"size\":${size},\"my_var1\":${x:var1},\"my_var2\":${x:var2}}");
- }
- });
- put.setCallbackVars(new HashMap<String, String>() {
- {
- put("x:var1", "value1");
- put("x:var2", "value2");
- }
- });