最近更新时间:2022-07-25
本文介绍如何使用分片上传。
分片上传(Multipart Upload)分为以下三个步骤:
初始化一个分片上传事件。
调用initiateMultipartUpload方法返回UOS创建的全局唯一的uploadId。
上传分片。
调用uploadPart方法上传分片数据。
完成分片上传。
所有分片上传完成后,调用completeMultipartUpload方法将所有分片合并成完整的文件。
以下通过一个完整的示例对分片上传的流程进行逐步解析:
public void testMutiPartUpload(){ File file = new File(filePath); long contentLength = file.length(); long partSize = 5 * 1024 * 1024; // Set part size to 5 MB. try { List // Initiate the multipart upload. InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, objectName); InitiateMultipartUploadResult initResponse = s3.initiateMultipartUpload(initRequest); // Upload the file parts. long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { // Because the last part could be less than 5 MB, adjust the part size as needed. partSize = Math.min(partSize, (contentLength - filePosition)); // Create the request to upload a part. UploadPartRequest uploadRequest = new UploadPartRequest() .withBucketName(bucketName) .withKey(objectName) .withUploadId(initResponse.getUploadId()) .withPartNumber(i) .withFileOffset(filePosition) .withFile(file) .withPartSize(partSize); // Upload the part and add the response's ETag to our list. UploadPartResult uploadResult = s3.uploadPart(uploadRequest); partETags.add(uploadResult.getPartETag()); filePosition += partSize; } // Complete the multipart upload. CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, objectName, initResponse.getUploadId(), partETags); s3.completeMultipartUpload(compRequest); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } } |
您可以调用abortMultipartUpload方法来取消分片上传事件。当一个分片上传事件被取消后,无法再使用这个uploadId做任何操作,已经上传的分片数据会被删除。
以下代码用于取消分片上传事件:
public void testAbortMutiPartUpload() { try { // Find all in-progress multipart uploads. ListMultipartUploadsRequest allMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); MultipartUploadListing multipartUploadListing = s3.listMultipartUploads(allMultipartUploadsRequest); List System.out.println("Before deletions, " + uploads.size() + " multipart uploads in progress."); // Abort each upload. for (MultipartUpload u : uploads) { System.out.println("Upload in progress: Key = \"" + u.getKey() + "\", id = " + u.getUploadId()); s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, u.getKey(), u.getUploadId())); System.out.println("Upload deleted: Key = \"" + u.getKey() + "\", id = " + u.getUploadId()); } // Verify that all in-progress multipart uploads have been aborted. multipartUploadListing = s3.listMultipartUploads(allMultipartUploadsRequest); uploads = multipartUploadListing.getMultipartUploads(); System.out.println("After aborting uploads, " + uploads.size() + " multipart uploads in progress."); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } } |
以下代码用于简单列举正在上传的分片:
public void testListMutiPartUpload() { try { // Retrieve a list of all in-progress multipart uploads. ListMultipartUploadsRequest allMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); MultipartUploadListing multipartUploadListing = s3.listMultipartUploads(allMultipartUploadsRequest); List
// Display information about all in-progress multipart uploads. System.out.println(uploads.size() + " multipart upload(s) in progress."); for (MultipartUpload u : uploads) { System.out.println("Upload in progress: Key = \"" + u.getKey() + "\", id = " + u.getUploadId()); } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } } |