文档中心 > 分片上传
分片上传

最近更新时间:2022-09-03

本文介绍如何使用分片上传。

上传步骤

分片上传(Multipart Upload)分为以下三个步骤:

  1. 初始化一个分片上传事件。

  2. 调用initiateMultipartUpload方法返回OSS创建的全局唯一的uploadId。

  3. 上传分片。

  4. 调用uploadPart方法上传分片数据。

  5. 完成分片上传。

    所有分片上传完成后,调用completeMultipartUpload方法将所有分片合并成完整的文件。

以下通过一个完整的示例对分片上传的流程进行逐步解析:

  public void testMutipartUploadObject() {
   try {
       long contentLength = file.length();
       // 设置分片大小为5MB
       long partSize = 5 * 1024 * 1024;

       // 初始化分片上传
       InitiateMultipartUploadResult initiateResult = s3
              .initiateMultipartUpload(new InitiateMultipartUploadRequest(BUCKET_NAME, KEY)
                      .withCannedACL(CannedAccessControlList.Private)
                      .withStorageClass(StorageClass.Standard));

       long filePosition = 0;
       List<PartETag> partETags = new ArrayList<>();
       for (int i = 1; filePosition < contentLength; i++) {
           partSize = Math.min(partSize, (contentLength - filePosition));

           // 上传分片
           UploadPartRequest uploadRequest = new UploadPartRequest()
                  .withBucketName(BUCKET_NAME)
                  .withKey(KEY)
                  .withUploadId(initiateResult.getUploadId())
                  .withPartNumber(i)
                  .withFileOffset(filePosition)
                  .withFile(file)
                  .withPartSize(partSize);

           UploadPartResult uploadResult = s3.uploadPart(uploadRequest);
           partETags.add(uploadResult.getPartETag());

           filePosition += partSize;
      }

       // 完成分片上传
       CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(BUCKET_NAME, KEY,
               initiateResult.getUploadId(), partETags);
       s3.completeMultipartUpload(compRequest);

  } catch (AmazonS3Exception e) {
       System.out.print("Append object faild, err :" + e);
  }
}

取消分片上传事件

您可以调用abortMultipartUpload方法来取消分片上传事件。当一个分片上传事件被取消后,无法再使用这个uploadId做任何操作,已经上传的分片数据会被删除。

以下代码用于取消分片上传事件:

  public void testAbortMutiPartUpload() {
       try {

           ListMultipartUploadsRequest allMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
           MultipartUploadListing multipartUploadListing = s3.listMultipartUploads(allMultipartUploadsRequest);

           List<MultipartUpload> uploads = multipartUploadListing.getMultipartUploads();
           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 {
             ListMultipartUploadsRequest allMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
             MultipartUploadListing multipartUploadListing = s3.listMultipartUploads(allMultipartUploadsRequest);
             List<MultipartUpload> uploads = multipartUploadListing.getMultipartUploads();
 
             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());
        }
    }