Updated on 2024-09-05 GMT+08:00

Assembling Parts (SDK for Go)

Function

This API assembles the uploaded parts to compete the multipart upload. Before performing this operation, you cannot download the uploaded data. When assembling parts, you need to copy the additional message header information recorded during the multipart upload initiation to the object metadata. Such information is processed the same way the information in a common object upload is processed. In the case of assembling parts concurrently, last write wins is applied, but the time of last write is defined as the time when a multipart upload was initiated.

The uploaded parts occupy your storage as long as the multipart upload has not been aborted. You can assemble all or some of the uploaded parts to complete the multipart upload. Once the multipart upload is complete, the parts that are not assembled will be deleted and no longer occupy storage.

When assembling parts, OBS creates an object by putting part numbers in ascending order. If any object metadata is provided in the initiation of the multipart upload, OBS will associate the metadata with the object. After the multipart upload is complete, the parts will no longer exist. A part assembling request must contain the upload ID, part numbers, and a list of corresponding ETag values. In response to the request, the ETag that uniquely identifies the assembled parts is contained. This ETag is not the MD5 hash value of the entire object.

Restrictions

  • To assemble parts, you must be the bucket owner or have the required permission (obs:object:PutObject in IAM or PutObject in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
  • After a multipart upload is complete, the uploaded parts that are not assembled will be automatically deleted and cannot be recovered. Before assembling parts, use the API for listing uploaded parts to check all parts to ensure that no part is missed.

Method

func (obsClient ObsClient) CompleteMultipartUpload(input *CompleteMultipartUploadInput) (output *CompleteMultipartUploadOutput, err error)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

input

*CompleteMultipartUploadInput

Yes

Explanation:

Input parameters for assembling parts. For details, see Table 2.

Table 2 CompleteMultipartUploadInput

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

Yes

Explanation:

Bucket name

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain a period (.) and a hyphen (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets of the same name in the same region, no error will be reported and the bucket attributes comply with those set in the first creation request.

Default value:

None

Key

string

Yes

Explanation:

Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name.

For example, if the address for accessing the object is examplebucket.obs.eu-west-101.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt.

Value range:

The value must contain 1 to 1,024 characters.

Default value:

None

UploadId

string

Yes

Explanation:

Multipart upload ID, for example, 000001648453845DBB78F2340DD460D8

Value range:

The value must contain 32 characters.

Default value:

None

Parts

[]Part

Yes

Explanation:

List of parts to be assembled. For details, see Table 3.

EncodingType

string

No

Explanation:

Encoding type for Key in the response. If Key in the response contains control characters that are not supported by the XML 1.0 standard, you can specify this parameter to encode Key.

Value range:

url

Default value:

None. If you leave this parameter blank, encoding is not applied to Key.

Table 3 Part

Parameter

Type

Description

PartNumber

int

Explanation:

Part number

Value range:

An integer ranging from 1 to 10000

Default value:

None

ETag

string

Explanation:

Part ETag, which is a Base64-encoded, 128-bit MD5 value

Value range:

The value must contain 32 characters.

Default value:

None

Responses

Table 4 List of returned results

Parameter

Type

Description

output

*CompleteMultipartUploadOutput

Explanation:

Returned results. For details, see Table 5.

err

error

Explanation:

Error messages returned by the API

Table 5 CompleteMultipartUploadOutput

Parameter

Type

Description

StatusCode

int

Explanation:

HTTP status code

Value range:

A status code is a group of digits that can be 2xx (indicating successes) or 4xx or 5xx (indicating errors). It indicates the status of a response. For more information, see Status Code.

Default value:

None

RequestId

string

Explanation:

Request ID returned by the OBS server

Default value:

None

ResponseHeaders

map[string][]string

Explanation:

HTTP response headers

Default value:

None

ETag

string

Explanation:

ETag calculated based on the ETags of all assembled parts, which is a Base64-encoded, 128-bit MD5 value of an object. ETag is the unique identifier of the object content. It can be used to determine whether the object content is changed. For example, if the ETag value is A when an object is uploaded, but changes to B when the object is downloaded, it indicates that the object content has been changed. The ETag reflects changes to the object content, rather than the object metadata. An uploaded or copied object has a unique ETag after being encrypted using MD5.

Restrictions:

If an object is encrypted using server-side encryption, the ETag is not the MD5 value of the object.

Value range:

The value must contain 32 characters.

Default value:

None

Bucket

string

Explanation:

Name of the bucket in which parts are assembled

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain a period (.) and a hyphen (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets of the same name in the same region, no error will be reported and the bucket attributes comply with those set in the first creation request.

Default value:

None

Key

string

Explanation:

Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name.

For example, if the address for accessing the object is examplebucket.obs.eu-west-101.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt.

Value range:

The value must contain 1 to 1,024 characters.

Default value:

None

Location

string

Explanation:

URL of the generated object after part assembling

Example: https://example-Bucket.obs.regions.myhuaweicloud.com/example-Object

Default value:

None

VersionId

string

Explanation:

Version ID of the object obtained after part assembling. If versioning is enabled for the bucket, the object version ID will be returned.

Value range:

The value must contain 32 characters.

Default value:

None

SseHeader

SseCHeader or SseKmsHeader

Explanation:

Server-side encryption header. If SSE-C is used, see Table 6. If SSE-KMS is used, see Table 7.

EncodingType

string

Explanation:

Encoding type for Key in the response. If Key in the response contains control characters that are not supported by the XML 1.0 standard, you can specify this parameter to encode Key.

Value range:

url

Default value:

None. If you leave this parameter blank, encoding is not applied to Key.

Table 6 SseCHeader

Parameter

Type

Mandatory (Yes/No)

Description

Encryption

string

Yes if used as a request parameter

Explanation:

SSE-C used for encrypting objects

Value range:

AES256, indicating objects are encrypted using SSE-C

Default value:

None

Key

string

Yes if used as a request parameter

Explanation:

Key for encrypting the object when SSE-C is used

Restrictions:

The value is a Base64-encoded 256-bit key, for example, K7QkYpBkM5+hca27fsNkUnNVaobncnLht/rCB2o/9Cw=.

Default value:

None

KeyMD5

string

No if used as a request parameter

Explanation:

MD5 value of the key for encrypting objects when SSE-C is used. This value is used to check whether any error occurs during the transmission of the key.

Restrictions:

The value is encrypted by MD5 and then encoded by Base64, for example, 4XvB3tbNTN+tIEVa0/fGaQ==.

Default value:

None

Table 7 SseKmsHeader

Parameter

Type

Mandatory (Yes/No)

Description

Encryption

string

Yes if used as a request parameter

Explanation:

SSE-KMS used for encrypting objects

Value range:

kms, indicating objects are encrypted using SSE-KMS

Default value:

None

Key

string

No if used as a request parameter

Explanation:

ID of the KMS master key when SSE-KMS is used

Value range:

Valid value formats are as follows:

  1. regionID:domainID:key/key_id
  2. key_id

In the preceding formats:

  • regionID indicates the ID of the region where the key is used.
  • domainID indicates the ID of the account where the key is used. To obtain it, see How Do I Get My Account ID and User ID?
  • key_id indicates the ID of the key created on Data Encryption Workshop (DEW).

Default value:

  • If this parameter is not specified, the default master key will be used.
  • If there is no such a default master key, OBS will create one and use it by default.

Code Example

This example assembles the parts previously uploaded to bucket examplebucket.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
    "fmt"
    "os"
    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
    //Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html.
    ak := os.Getenv("AccessKeyID")
    sk := os.Getenv("SecretAccessKey")
    // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding to reduce leakage risks. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
    // securityToken := os.Getenv("SecurityToken")
    // Enter the endpoint corresponding to the bucket. EU-Dublin is used here as an example. Replace it with the one currently in use.
    endPoint := "https://obs.eu-west-101.myhuaweicloud.eu" 
    // Create an obsClient instance.
    // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance.
    obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
    input := &obs.CompleteMultipartUploadInput{}
    // Specify a bucket name.
    input.Bucket = "examplebucket"
    // Specify an object name (example/objectname as an example).
    input.Key = "example/objectname"
    // Specify the multipart upload ID (00000188677110424014075CC4A77xxx as an example).
    input.UploadId = "00000188677110424014075CC4A77xxx"
    // Specify the list of parts to be assembled.
    input.Parts = []obs.Part{
        {PartNumber: 1, ETag: "etag1"},
        {PartNumber: 2, ETag: "etag2"},
        {PartNumber: 3, ETag: "etag3"},
    }
    // Complete the multipart upload.
    output, err := obsClient.CompleteMultipartUpload(input)
    if err == nil {
        fmt.Printf("Complete multipart upload successful with bucket(%s) and object(%s)!\n", input.Bucket, input.Key)
        fmt.Printf("Location:%s, Bucket:%s, Key:%s, ETag:%s\n", output.Location, output.Bucket, output.Key, output.ETag)
        return
    }
    fmt.Printf("Complete multipart upload(%s) fail with bucket(%s) and object(%s)!\n", input.Bucket, input.Key)
    if obsError, ok := err.(obs.ObsError); ok {
        fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.")
        fmt.Println(obsError.Error())
    } else {
        fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.")
        fmt.Println(err)
    }
}