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

最近更新时间:2021-09-22

分片上传是指将要上传的文件分成若干个分片(Part)分别上传,所有分片都上传完成后,将所有分片合并成完整的文件,完成整个文件的上传。

首先会初始化一个UploadID参数,每上传问一个分片文件会生成ETag参数,如果本片上传失败会重新上传该分片。最后判断ETag和part数量一致,将所有的分片合成完整的文件完成上传。


分片上传

以下代码用于分片上传文件:

func MultiPartUploadSample() {

DeleteTestBucketAndObject()

defer DeleteTestBucketAndObject()

sc := s3lib.NewS3(endpoint, accessKey, secretKey)

// Create a bucket

err := sc.MakeBucket(bucketName)

if err != nil {

          HandleError(err)

}

 

// 1.Create Multipart Upload

uploadId,err := sc.CreateMultiPartUpload(bucketName, objectKey,s3.ObjectStorageClassStandard)

if err != nil {

          HandleError(err)

}

fmt.Println("UploadId is: ",aws.StringValue(uploadId))

partCount := 3

completedUpload := &s3.CompletedMultipartUpload{

          Parts: make([]*s3.CompletedPart, partCount),

}

for i := 0; i < partCount; i++ {

          partNumber := int64(i + 1)

          etag,err:=sc.UploadPart(bucketName, objectKey, partNumber,uploadId, GenMinimalPart())

          if err != nil {

                   HandleError(err)

          }

          completedUpload.Parts[i] = &s3.CompletedPart{

                   ETag:       aws.String(etag),

                   PartNumber: aws.Int64(partNumber),

          }

}

// 2.Upload Part

err = sc.CompleteMultiPartUpload(bucketName, objectKey, completedUpload, uploadId)

if err != nil {

          HandleError(err)

          err = sc.AbortMultiPartUpload(bucketName, objectKey, uploadId)

          if err != nil {

                   HandleError(err)

          }

}

fmt.Printf("MultipartUploadSample Run Success !\n\n")

 

}


禁止覆盖同名分片上传

package sample

 

import (

"fmt"

. "github.com/journeymidnight/Yig-S3-SDK-Go/s3lib"

"github.com/journeymidnight/aws-sdk-go/aws"

"github.com/journeymidnight/aws-sdk-go/service/s3"

)

 

func MultiPartUploadSampleWithForbidOverwrite() {

DeleteTestBucketAndObject()

defer DeleteTestBucketAndObject()

sc := NewS3(endpoint, accessKey, secretKey)

// Create a bucket

err := sc.MakeBucket(bucketName)

if err != nil {

          HandleError(err)

}

 

uploadId, err := sc.CreateMultiPartUpload(bucketName, objectKey, s3.ObjectStorageClassStandard)

if err != nil {

          HandleError(err)

}

fmt.Println("UploadId is: ", aws.StringValue(aws.String(uploadId)))

partCount := 3

completedUpload := &s3.CompletedMultipartUpload{

          Parts: make([]*s3.CompletedPart, partCount),

}

for i := 0; i < partCount; i++ {

          partNumber := int64(i + 1)

          etag, err := sc.UploadPart(bucketName, objectKey, GenMinimalPart(), uploadId, partNumber)

          if err != nil {

                   HandleError(err)

          }

          completedUpload.Parts[i] = &s3.CompletedPart{

                   ETag:       aws.String(etag),

                   PartNumber: aws.Int64(partNumber),

          }

}

err = sc.CompleteMultiPartUpload(bucketName, objectKey, uploadId, completedUpload)

if err != nil {

          HandleError(err)

          err = sc.AbortMultiPartUpload(bucketName, objectKey, uploadId)

          if err != nil {

                   HandleError(err)

          }

}

fmt.Printf("MultipartUploadSample Run Success !\n\n")

 

// forbid overwrite

uploadId, err = sc.CreateMultiPartUploadWithForbidOverwrite(bucketName, objectKey, s3.ObjectStorageClassStandard, true)

if err == nil {

          fmt.Println("forbid overwrite success:", err)

}

 

// overwrite

uploadId, err = sc.CreateMultiPartUploadWithForbidOverwrite(bucketName, objectKey, s3.ObjectStorageClassStandard,false)

if err != nil {

          HandleError(err)

}

fmt.Println("UploadId is: ", aws.StringValue(aws.String(uploadId)))

partCount = 5

completedUpload = &s3.CompletedMultipartUpload{

          Parts: make([]*s3.CompletedPart, partCount),

}

for i := 0; i < partCount; i++ {

          partNumber := int64(i + 1)

          etag, err := sc.UploadPart(bucketName, objectKey, GenMinimalPart(), uploadId, partNumber)

          if err != nil {

                   HandleError(err)

          }

          completedUpload.Parts[i] = &s3.CompletedPart{

                   ETag:       aws.String(etag),

                   PartNumber: aws.Int64(partNumber),

          }

}

err = sc.CompleteMultiPartUpload(bucketName, objectKey, uploadId, completedUpload)

if err != nil {

          HandleError(err)

          err = sc.AbortMultiPartUpload(bucketName, objectKey, uploadId)

          if err != nil {

                   HandleError(err)

          }

}

 

fmt.Printf("overwrite Run Success !\n\n")

}