Updated on 2024-06-19 GMT+08:00

Assembling Parts (SDK for Python)

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.
  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • 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

ObsClient.completeMultipartUpload(bucketName, objectKey, uploadId, completeMultipartUploadRequest)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

bucketName

str

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 periods (.) and hyphens (-) 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

objectKey

str

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.ap-southeast-1.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

NOTE:

The object URL is in the following format: https://Bucket name.Domain name/Folder directory level/Object name. If this object is stored in the root directory of the bucket, its URL does not contain the folder directory level.

uploadId

str

Yes

Explanation:

Multipart upload ID, for example, 000001648453845DBB78F2340DD460D8

Value range:

The value must contain 32 characters.

Default value:

None

completeMultipartUploadRequest

CompleteMultipartUploadRequest

Yes

Explanation:

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

encoding_type

str

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 2 CompleteMultipartUploadRequest

Parameter

Type

Mandatory (Yes/No)

Description

parts

list of CompletePart

Yes

Explanation:

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

Table 3 CompletePart

Parameter

Type

Mandatory (Yes/No)

Description

partNum

int

Yes

Explanation:

Part number

Value range:

[1,10000]

Default value:

None

etag

str

Yes

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

Type

Description

GetResult

Explanation:

SDK common results

Table 5 GetResult

Parameter

Type

Description

status

int

Explanation:

HTTP status code

Value range:

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

Default value:

None

reason

str

Explanation:

Reason description.

Default value:

None

errorCode

str

Explanation:

Error code returned by the OBS server. If the value of status is less than 300, this parameter is left blank.

Default value:

None

errorMessage

str

Explanation:

Error message returned by the OBS server. If the value of status is less than 300, this parameter is left blank.

Default value:

None

requestId

str

Explanation:

Request ID returned by the OBS server

Default value:

None

indicator

str

Explanation:

Error indicator returned by the OBS server.

Default value:

None

hostId

str

Explanation:

Requested server ID. If the value of status is less than 300, this parameter is left blank.

Default value:

None

resource

str

Explanation:

Error source (a bucket or an object). If the value of status is less than 300, this parameter is left blank.

Default value:

None

header

list

Explanation:

Response header list, composed of tuples. Each tuple consists of two elements, respectively corresponding to the key and value of a response header.

Default value:

None

body

object

Explanation:

Result content returned after the operation is successful. If the value of status is larger than 300, this parameter value is null. The value varies with the API being called. For details, see Bucket-Related APIs (SDK for Python) and Object-Related APIs (SDK for Python).

Default value:

None

Table 6 GetResult.body

GetResult.body Type

Description

CompleteMultipartUploadResponse

Explanation:

Response to the request for assembling parts. For details, see Table 7.

Table 7 CompleteMultipartUploadResponse

Parameter

Type

Description

etag

str

Explanation:

ETag calculated based on the ETags of all assembled parts

Explanation:

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 is A when an object is uploaded and is B when the object is downloaded, the object content is changed. The ETag reflects changes to the contents of the object, not its metadata. An uploaded object or copied object has a unique ETag.

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

str

Explanation:

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 periods (.) and hyphens (-) 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

str

Explanation:

Object name obtained after part assembling.

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.ap-southeast-1.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

str

Explanation:

URL of the generated object after part assembling

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

Default value:

None

versionId

str

Explanation:

Version ID of the object obtained after part assembling

Value range:

The value must contain 32 characters.

Default value:

None

sseKms

str

Explanation:

SSE-KMS algorithm

Value range:

kms

Default value:

None

sseKmsKey

str

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:

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.

sseC

str

Explanation:

SSE-C algorithm

Value range:

AES256

Default value:

None

sseCKeyMd5

str

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

objectUrl

str

Explanation:

Full path to the obtained object after part assembling

Default value:

None

encoding_type

str

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.

Code Examples

This example assembles the parts previously uploaded to a bucket.

 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
from obs import ObsClient
from obs import CompleteMultipartUploadRequest, CompletePart
import os
import traceback

# Obtain an AK and SK pair using environment variables or import the AK and SK pair in other ways. Using hard coding may result in leakage.
# Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them from environment variables.
security_token = os.getenv("SecurityToken")
# Set server to the endpoint corresponding to the bucket. Here uses CN-Hong Kong as an example. Replace it with the one in use.
server = "https://obs.ap-southeast-1.myhuaweicloud.com" 

# Create an obsClient instance.
# If you use a temporary AK and SK pair and a security token to access OBS, you must specify security_token when creating an instance.
obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
    # Specify the list of parts to be assembled by configuring completeMultipartUploadRequest. Each part contains partNum and etag.
    part1 = CompletePart(partNum='partNumber1', etag='etag1')
    part2 = CompletePart(partNum='partNumber2', etag='etag2')
    completeMultipartUploadRequest = CompleteMultipartUploadRequest(parts=[part1, part2])

    bucketName = "examplebucket"
    objectKey = "objectname"
    # Specify the ID of the multipart upload.
    uploadId = "your uploadid"
    # Assemble the parts uploaded to the bucket.
    resp = obsClient.completeMultipartUpload(bucketName, objectKey, uploadId, completeMultipartUploadRequest, encoding_type='url')

    # If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
    if resp.status < 300:
        print('Complete Multipart Upload Succeeded')
        print('requestId:', resp.requestId)
        print('etag:', resp.body.etag)
        print('bucket:', resp.body.bucket)
        print('key:', resp.body.key)
        print('location:', resp.body.location)
        print('versionId:', resp.body.versionId)
    else:
        print('Complete Multipart Upload Failed')
        print('requestId:', resp.requestId)
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    print('Complete Multipart Upload Failed')
    print(traceback.format_exc())