标准接口

标准接口由四个接口组合完成分片上传文件的功能,initiateMultipartUpload,uploadPart,completeMultipartUpload,abortMultipartUpload。

创建分片上传任务

创建分片上传任务,返回分片上传任务的ID。

接口定义:

public InitiateMultipartUploadResult initiateMultipartUpload(
            InitiateMultipartUploadRequest initiateMultipartUploadRequest)

参数:

参数名 类型 说明
bucketName String bucket名
key String 要上传的对象名
cannedACL CannedAccessControlList 权限设置
objectMetadata ObjectMetadata 可选参数,元数据,可以设置ContentType和自定义元数据

代码示例:

public void InitiateMultipartUpload(String bucketName, String objectKey,
	OnS3ResponseListener<InitiateMultipartUploadResult> listener) {
	GlobalThreadPool.getInstance().execute(() -> {
		try {
			InitiateMultipartUploadRequest initiateMultipartUploadRequest =
					new InitiateMultipartUploadRequest(bucketName, objectKey);
			InitiateMultipartUploadResult result = sS3Client.initiateMultipartUpload(initiateMultipartUploadRequest);
			Handler mainHandler = new Handler(Looper.getMainLooper());
			mainHandler.post(() -> listener.onResponse(result));
		} catch (Exception e) {
			Handler mainHandler = new Handler(Looper.getMainLooper());
			mainHandler.post(() -> listener.onError());
		}
	});
}

上传一个分片

获取到分片任务ID之后,通过ID来上传分片内容到S3服务器。

接口定义:

public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest)

参数:

参数名 类型 说明
bucketName String bucket名
key String 要上传的对象名称
file FILE 上传的文件对象
uploadId String 上传任务ID
partSize long 上传内容长度
partNum int 分片ID(1-10000)
fileOffset long 文件起始位置

代码示例:

public void UploadPart(String bucketName, String objectKey, String uploadId,
	int partNum, File file, int filePosition, int partSize,
	OnS3ResponseListener<UploadPartResult> listener) {
	GlobalThreadPool.getInstance().execute(() -> {
		try {
			UploadPartRequest uploadPartRequest = new UploadPartRequest()
					.withBucketName(bucketName)
					.withKey(objectKey)
					.withUploadId(uploadId)
					.withPartNumber(partNum)
					.withFileOffset(filePosition)
					.withFile(file)
					.withPartSize(partSize);
			UploadPartResult result = sS3Client.uploadPart(uploadPartRequest);
			Handler mainHandler = new Handler(Looper.getMainLooper());
			mainHandler.post(() -> listener.onResponse(result));
		} catch (Exception e) {
			Handler mainHandler = new Handler(Looper.getMainLooper());
			mainHandler.post(() -> listener.onError());
		}
	});
}

完成分片上传任务

完成所有分片的上传之后,调用完成接口,服务端会把所有分片合并成对象保存。

接口定义:

public CompleteMultipartUploadResult completeMultipartUpload(
            CompleteMultipartUploadRequest completeMultipartUploadRequest)

参数:

参数名 类型 说明
bucketName String bucket名
key String 要上传的对象名
uploadId String 上传任务ID
partETags List 上传的分片信息列表

代码示例:

public void CompleteMultipartUpload(String bucketName,
	String objectKey,
	String uploadId,
	List<PartETag> partETags,
	OnS3ResponseListener<CompleteMultipartUploadResult> listener) {
	GlobalThreadPool.getInstance().execute(() -> {
		try {
			CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(
					bucketName, objectKey, uploadId, partETags);
			CompleteMultipartUploadResult result = sS3Client.completeMultipartUpload(completeMultipartUploadRequest);
			Handler mainHandler = new Handler(Looper.getMainLooper());
			mainHandler.post(() -> listener.onResponse(result));
		} catch (Exception e) {
			Handler mainHandler = new Handler(Looper.getMainLooper());
			mainHandler.post(() -> listener.onError());
		}
	});
}

终止分片上传任务

上传失败的时候调用此接口,服务器会清除残留的分片数据。

接口定义:

public void abortMultipartUpload(AbortMultipartUploadRequest abortMultipartUploadRequest)

参数:

参数名 类型 说明
bucketName String bucket名
key String 要上传的对象名
uploadId String 上传任务ID

代码示例:

public void AbortMultipartUpload(String bucketName, String objectKey, String uploadId) {
	GlobalThreadPool.getInstance().execute(() -> {
		try {
			AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(
					bucketName, objectKey, uploadId);
			sS3Client.abortMultipartUpload(abortMultipartUploadRequest);
		} catch (Exception e) {
		}
	});
}