最近更新时间:2023-06-06 17:17:50
以下为实现初始化分块上传代码:
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest("<bucketName>", "<fileName>");
client.initiateMultipartUpload(request, new InitiateMultipartUploadResponceHeader() {
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, InitiateMultipartUploadResult result){
// 请求成功后 会返回一个InitiateMultipartUploadResult实例对象,通过该对象可以获取上传的uploadId,用于后续的Upload Part 请求。
}
@Override
public void onFailure(int statesCode, Ks3Error error, Header[] responceHeaders, String response, Throwable paramThrowable) {
// 请求异常
}
});
以下为分块上传文件示例代码:
// file:需要上传的文件
// PART_SIZE: 此分块的大小,单位为Byte
UploadPartRequestFactory requestFactory = new UploadPartRequestFactory("<bucketName>", "<ObjectKey>","<uploadId>", file, PART_SIZE);
UploadPartRequest request = requestFactory.getNextUploadPartRequest();
client.uploadPart(request, new UploadPartResponceHandler() {
@Override
public void onTaskProgress(double progress) {
//上传进度
}
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, PartETag result) {
// 上传成功时返回PartETag结果实体类,包含partNumber以及Etag信息
}
@Override
public void onFailure(int statesCode, Ks3Error error, Header[] responceHeaders, String response, Throwable throwable) {
//上传异常
}
});
// 当然你也可以自行上传文件,以下为参数说明:
// bucketname:指定的Bucket名称
// objectKey:指定的Object键
// uploadId:初始化时得到的uploadId
// file:需要上传的file
// offset:此分块在文件中内容的起始位置
// partNumber:此分块是第几块
// partSize:此分块的大小,单位为Byte
client.uploadPart("<bucketName>", "<objectKey>", "<uploadId>", "<file>", "<offset>", "<partNumber>", "<partSize>", new UploadPartResponceHandler() {...});
注:该接口请求实现将对象按照分块的方式上传到 KS3。最多支持10000分块,当所有块总大小大于5M时,除了最后一个块没有大小限制外,其余的块的大小均要求在5MB以上。当所有块总大小小于5M时,除了最后一个块没有大小限制外,其余的块的大小均要求在100K以上。如果不符合上述要求,会返回413状态码。
以下为获取已经上传的块信息:
ListPartsRequest request = new ListPartsRequest("<bucketName>", "<objectKey>", "<uploadId>");
client.listParts(request, new ListPartsResponseHandler() {
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, ListPartsResult result) {
// 请求成功返回ListPartsResult结果实体类,包含Owner及一个Parts容器类
}
@Override
public void onFailure(int statesCode, Ks3Error error, Header[] responceHeaders, String response, Throwable paramThrowable) {
//请求异常
}
});
以下为取消分块上传的示例代码:
AbortMultipartUploadRequest request = new AbortMultipartUploadRequest( "<bucketName>", "<objectKey>", "<uploadId>");
client.abortMultipartUpload(request, new AbortMultipartUploadResponseHandler() {
@Override
public void onSuccess(int statesCode, Header[] responceHeaders) {
// responceHeaders: Http请求响应报头
}
@Override
public void onFailure(int statesCode, Ks3Error error, Header[] responceHeaders, String response, Throwable paramThrowable) {
//请求失败
}
});
注:当任务被放弃后,使用相应的上传ID则会无法实现任何块的上传。存储中已经上传的块将会被释放。
以下为组装之前上传的块示例代码:
// listResult为ListPartsResult实例对象
CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(listResult);
client.completeMultipartUpload(request, new CompleteMultipartUploadResponseHandler() {
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, CompleteMultipartUploadResult result){
// 组装成功 可以通过result获取当前bucket、eTag、key、location信息
}
@Override
public void onFailure(int statesCode, Ks3Error error, Header[] responceHeaders, String response, Throwable paramThrowable) {
// 请求异常
}
});
注:该操作执行成功后,对应的uploadId将会失效。同一个Object可以拥有不同的uploadId,当组装操作完成后,该Object的其他uploadId不受影响。
UploadPartHandler handler = new UploadPartHandler();
client.initiateMultipartUpload(new InitiateMultipartUploadRequest("<bucketName>", "<objectKey>"), new InitiateMultipartUploadResponceHandler() {
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, InitiateMultipartUploadResult result) {
String uploadId = result.getUploadId();
// 初始化分块上传 得到uploadId
// 发送消息 执行下一步操作
handler.sendEmptyMessage(TransferManager.STATE_UPLOAD_NEXT);
}
@Override
public void onFailure(int statesCode, Header[] responceHeaders, String response, Throwable paramThrowable) {
// 请求异常
}
}
);
public class UploadPartHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case TransferManager.STATE_UPLOAD_NEXT:
// 如果有未完成的请求 则继续执行
if (localUploadPartRequestFactory.hasMoreRequests()) {
UploadPartRequest uploadPartRequest = localUploadPartRequestFactory.getNextUploadPartRequest();
client.uploadPart(uploadPartRequest, new UploadPartResponceHandler() {
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, UploadPartResult result) {
//请求成功
Log.d("eflake", "upload part success , partnumber = " + result.getPartNumber() + ",etag = " + result.getETag());
handler.sendEmptyMessage(TransferManager.STATE_UPLOAD_NEXT);
}
@Override
public void onFailure(int statesCode, Header[] responceHeaders, String response, Throwable throwable) {
//请求异常
Log.d("eflake", "upload part failed ,responce = " + response);
handler.sendEmptyMessage(TransferManager.STATE_UPLOAD_FAILED);
}
});
} else {
// 发送完成消息
handler.sendEmptyMessage(TransferManager.STATE_UPLOAD_FINISH);
}
break;
case TransferManager.STATE_UPLOAD_FINISH:
// 获取分块信息
client.ListParts(bucketName, objectKey, transferResponceHandler.getMultipartUoloadId(), new ListPartsResponseHandler() {
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, ListPartsResult listPartsResult) {
//请求成功
mLastListPartResul = listPartsResult;
handler.sendEmptyMessage(TransferManager.STATE_UPLOAD_COMPLETE);
}
@Override
public void onFailure(int statesCode, Header[] responceHeaders, String response, Throwable paramThrowable) {
//请求异常
Log.d("eflake", "listPart failed, reason :" + response);
}
});
break;
case TransferManager.STATE_UPLOAD_FAILED:
break;
case TransferManager.STATE_UPLOAD_COMPLETE:
if (mLastListPartResul != null) {
// 将分块数据进行合并处理
client.completeMultipartUpload(new CompleteMultipartUploadRequest(mLastListPartResul),new CompleteMultipartUploadResponseHandler() {
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, CompleteMultipartUploadResult result) {
//请求成功
Log.d("eflake", "completeMultipartUpload success, result = " + result.getBucket());
}
@Override
public void onFailure(int statesCode, Header[] responceHeaders, String response, Throwable paramThrowable) {
//请求异常
Log.d("eflake", "completeMultipartUpload failed, reason = " + response);
}
});
}
break;
default:
break;
}
}
}
以上接口是比较底层的,需要自行创建uploadid,上传每一块。com.ksyun.ks3.services.MultiUploader这个类是更高层封装。一般大于10M的文件才使用分块。本接口是按5M一块分块的。以下为示例代码:
Ks3Client client = Ks3ClientFactory.getDefaultClient(MultiUploadActivity.this);
MultiUploader uploader = new MultiUploader(client, "<bucketName>", "<objectKey>", file, "<uploadId>");
// 设置回调函数 监听是否成功
uploader.setHandler(new CompleteMultipartUploadResponseHandler() {
@Override
public void onFailure(int statesCode, Ks3Error error, Header[] responceHeaders, String response, Throwable paramThrowable) {
// 整个上传过程中 如果任意环节出现异常 都会执行到此处
}
@Override
public void onSuccess(int statesCode, Header[] responceHeaders, CompleteMultipartUploadResult result) {
// 最终的成功 才会执行到此
}
});
uploader.upload();
注:可以参考sdk源码中的com.ksyun.ks3.services.MultiUploader.java自行实现,也可以将该类拷贝到自己的目录下,修改partSize变量、log输出格式等之后再使用。
纯净模式