Skip to content

Latest commit

 

History

History
59 lines (49 loc) · 1.3 KB

upload.md

File metadata and controls

59 lines (49 loc) · 1.3 KB

Multipart Upload

OSS supports upload a file via multiple API calls: Multipart Upload.

Init multipart upload

	initResult, err := api.InitUpload("bucket-name", "object/name")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%#v\n", initResult)
	uploadID = initResult.UploadID // remember this uploadID

Upload parts

	var parts []oss.Part
	for partNumber := 0; partNumber < maxPartNumber; partNumber++ {
		partcontents := fmt.Sprintf("contents of part %d\n", partNumber)
		partSize := int64(len(partContents))
		partResult, err := UploadPart("bucket-name", "object/name", uploadID, partNumber, strings.NewReader(partContents), partSize)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%#v\n", partResult)

		// collect parts information
		parts = append(parts, oss.Part{
			PartNumber: partNumber,
			LastModified: partResult.LastModified,
			ETag: partResult.ETag,
			Size: partSize,
		})
	}

Complete upload after all parts are uploaded

	res, err := api.CompleteUpload("bucket-name", "object/name", uploadID, &oss.CompleteMultipartUpload{
		Parts: parts,
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%#v\n", res)

Abort the multipart upload

	err := api.AbortUpload("bucket-name", "object/name", uploadID)
	if err != nil {
		log.Fatal(err)
	}