Help Center> Object Storage Service> SDK Reference> Go> Objects-Related APIs> Uploading Objects - Resumable Transfer

Uploading Objects - Resumable Transfer

API Description

You can use this API to encapsulate and enhance multipart upload in response to an upload failure of large files due to unstable network or program breakdown. The working principle is to divide the to-be-uploaded file into multiple parts and upload them separately. The upload result of each part is recorded in a checkpoint file in real time. Only when all parts are successfully uploaded, the result indicating a successful upload is returned. Otherwise, an exception is thrown to remind you of calling the API again for re-uploading. Based on the upload status of each part recorded in the checkpoint file, the re-uploading will upload the parts failed to be uploaded previously, instead of uploading all parts. By virtue of this, resources are saved and efficiency is improved.

You can call ObsClient.UploadFile to perform a resumable upload. The following table describes the parameters involved in this API.

Parameter

Description

Bucket

(Mandatory) Bucket name

Key

(Mandatory) Object name

UploadFile

(Mandatory) Local file to be uploaded

PartSize

Part size, in bytes. The value ranges from 100 KB to 5 GB.

TaskNum

Maximum number of files that can be uploaded concurrently in multipart mode.

EnableCheckpoint

Whether to enable the resumable upload mode. The default value is False, indicating that this mode is disabled.

CheckpointFile

File used to record the upload progress. This parameter is effective only in the resumable upload mode. If the value of this parameter is empty, the file will be in the same directory as the local file to be uploaded.

Metadata

Object metadata

ContentType

MIME type of the object

ACL

Pre-defined access control policies specified during object upload

StorageClass

Storage class, which can be specified during object upload

Method Definition

func (obsClient ObsClient) UploadFile(input *UploadFileInput) (output *CompleteMultipartUploadOutput, err error)

Request Parameters

Field

Type

Optional or Mandatory

input

*UploadFileInput

Mandatory

Returned Results

Field

Type

output

*CompleteMultipartUploadOutput

err

error

Sample Code

func main() {
       input := &obs.UploadFileInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       input.UploadFile = "localfile"   // localfile is the path of the local file to be uploaded. The file name must be specified.
       input.EnableCheckpoint = true    // Enable the resumable download mode.
       input.PartSize = 9 * 1024 * 1024  // Set the part size to 9 MB.
       input.TaskNum = 5  // Specify the maximum number of parts that can be concurrently uploaded.
       output, err := obsClient.UploadFile(input)
       if err == nil {
              fmt.Printf("RequestId:%s\n", output.RequestId)
              fmt.Printf("ETag:%s\n", output.ETag)
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}