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

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

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

上传文件

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

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

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

  2. 上传分片。

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

  3. 完成分片上传。

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

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

let key = "multipart-test"
var uploadId : String?
var parts : Array<AWSS3CompletedPart>?

// 初始化分片上传
let createMultipartReq = AWSS3CreateMultipartUploadRequest.init()
createMultipartReq?.bucket = S3BucketName
createMultipartReq?.key = key
s3.createMultipartUpload(createMultipartReq!).continueWith(block: { (task:AWSTask<AWSS3CreateMultipartUploadOutput>) -> Any? in
   if let error = task.error {
       print("create multipart request failed with error: (\(error))")
   } else {
       uploadId = task.result?.uploadId
       print("create muiltipart completed!" + uploadId!)
   }
   return nil
})

// 上传一个分片
let uploadPartReq = AWSS3UploadPartRequest.init()
let databody:NSData? = "MY TEST MULTIPART UPLOAD!".data(using: .utf8) as NSData?
uploadPartReq?.bucket = S3BucketName
uploadPartReq?.key = key
uploadPartReq?.partNumber = 1
uploadPartReq?.body = databody
s3.uploadPart(uploadPartReq!).continueWith(block: { (task:AWSTask<AWSS3UploadPartOutput>) -> Any? in
   if let error = task.error {
       print("upload part failed with error: (\(error))")
   } else {
       let info = AWSS3CompletedPart.init()
       info?.eTag = task.result?.eTag
       info?.partNumber = 1
       parts = [info!]
       print("upload completed.")
   }
   return nil
})

// 完成分片上传
let completinfo = AWSS3CompletedMultipartUpload.init()
if parts == nil {
   print("no part")
   return
}
completinfo?.parts = parts ?? []
let complete = AWSS3CompleteMultipartUploadRequest.init()
complete?.bucket = S3BucketName
complete?.key = key
complete?.uploadId = uploadId
complete?.multipartUpload = completinfo
s3.completeMultipartUpload(complete!).continueWith(block: { (task) -> Any? in
   if let error = task.error {
       print("multipart upload failed with error: (\(error))")
   } else {
       print("multipart upload completed!")
   }
   return nil
})

取消分片上传事件

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

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

var uploads : Array<AWSS3MultipartUpload>?
let listpartreq = AWSS3ListMultipartUploadsRequest.init()
listpartreq?.bucket = S3BucketName
s3.listMultipartUploads(listpartreq!).continueWith(block: { (task:AWSTask<AWSS3ListMultipartUploadsOutput>) -> Any? in
   if let error = task.error {
       print("list parts failed with error: (\(error))")
   } else {
       let count = task.result?.uploads?.count
       if count == nil {
           print("there not have multipart upload(s) in progress.")
           uploads = Array.init()
           return nil
       }s
       // 取消分片
       uploads = task.result?.uploads
       for upload in uploads! {
           let abortreq = AWSS3AbortMultipartUploadRequest.init()
           abortreq?.bucket = self.S3BucketName
           abortreq?.key = upload.key
           abortreq?.uploadId = upload.uploadId
           s3.abortMultipartUpload(abortreq!).continueWith(block: { (task) -> Any? in
               if let error = task.error {
                   print("abort parts failed with error: (\(error))")
               } else {
                   print("abort parts completed!")
               }
               return nil
           })
       }
   }
   return nil
})

列举正在上传的分片

以下代码用于简单列举正在上传的分片:

let listpartreq = AWSS3ListMultipartUploadsRequest.init()
listpartreq?.bucket = S3BucketName
s3.listMultipartUploads(listpartreq!).continueWith(block: { (task:AWSTask<AWSS3ListMultipartUploadsOutput>) -> Any? in
   if let error = task.error {
       print("list parts failed with error: (\(error))")
   } else {
       let count = task.result?.uploads?.count
       if count == nil {
           print(" there not have multipart upload(s) in progress.")
           return nil
       }
       print(" \(count!) multipart upload(s) in progress.")
       print("list parts completed!")
   }
   return nil
})