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

Uploading a Part (SDK for Go)

Function

After a multipart upload is initiated, this API uploads a part to a specified bucket. In the upload request, the multipart upload ID must be included. Except for the part lastly being uploaded whose size ranges from 0 to 5 GB, sizes of the other parts range from 100 KB to 5 GB. Part numbers can be any number from 1 to 10,000.

When uploading a part, you must specify its upload ID and part number. A part number uniquely identifies a part and its position in the object you are uploading. If you upload a new part with the same part number as that of a previous part, the previously uploaded part will be overwritten. Whenever you upload a part, OBS returns the ETag header in the response. For each part upload task, you must record the part number and ETag value. These values are required in subsequent requests for you to complete a multipart upload.

Restrictions

  • To upload a part, 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 initiating a multipart upload and uploading one or more parts, you must assemble the parts or abort the multipart upload. Only after that can OBS stops billing you for storing the uploaded parts.
  • partNumber in a multipart upload must be unique. When the same partNumber of the same object is concurrently uploaded, last write wins policy is applied. The time of last write is defined as the time when the part metadata is created. To ensure data accuracy, the client must be locked to ensure concurrent uploads of the same part of the same object. Concurrent uploads for different parts of the same object do not need to be locked.

Method

func (obsClient ObsClient) UploadPart(input *UploadPartInput) (output *UploadPartOutput, err error)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

input

*UploadPartInput

Yes

Explanation:

Input parameters for uploading a part. For details, see Table 2.

Table 2 UploadPartInput

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

PartNumber

int

Yes

Explanation:

Part number

Value range:

An integer ranging from 1 to 10000

Default value:

None

UploadId

string

Yes

Explanation:

Multipart upload ID which can be returned by initiating a multipart upload

Restrictions:

Value range:

The value must contain 32 characters.

Example: 000001648453845DBB78F2340DD460D8

Default value:

None

ContentMD5

string

No

Explanation:

Base64-encoded MD5 value of the object data to be uploaded. It is provided for the OBS server to verify data integrity.

Value range:

Base64-encoded 128-bit MD5 value of the request body calculated according to RFC 1864

Example: n58IG6hfM7vqI4K0vnWpog==

Default value:

None

SseHeader

SseCHeader

No

Explanation:

Server-side encryption header. If SSE-C is used, see Table 3.

Body

io.Reader

No

Explanation:

Data stream of the object to be uploaded

Value range:

The value ranges from 0 TB to 48.8 TB, in bytes.

Default value:

None

SourceFile

string

No

Explanation:

Source file path of the object to be uploaded

Default value:

None

Offset

int64

No

Explanation:

Start offset of a part in the source file

Value range:

A non-negative integer smaller than the size of the object to be uploaded, in bytes

Default value:

0

PartSize

int64

No

Explanation:

Part size

Value range:

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

Default value:

102400

Table 3 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

Responses

Table 4 List of returned results

Parameter

Type

Description

output

*UploadPartOutput

Explanation:

Returned results. For details, see Table 5.

err

error

Explanation:

Error messages returned by the API

Table 5 UploadPartOutput

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

PartNumber

int

Explanation:

Part number

Value range:

An integer ranging from 1 to 10000

Default value:

None

ETag

string

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

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.

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 uploads a part to bucket examplebucket by specifying a multipart upload ID.

 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
package main
import (
    "fmt"
    "os"
    "strings"
    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.UploadPartInput{}
    // 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 part number (1 as an example) of part to upload. The part number can be any integer from 1 to 10,000.
    input.PartNumber = 1
    // Specify the source file path (localfile as an example) of the part to upload.
    input.SourceFile = "localfile"
    // Upload the part.
    output, err := obsClient.UploadPart(input)
    if err == nil {
        fmt.Printf("Upload part(%d) successful with bucket(%s) and object(%s)!!\n", input.PartNumber, input.Bucket, input.Key)
        fmt.Printf("ETag:%s\n", output.ETag)
        return
    }
    fmt.Printf("Upload part(%d) fail with bucket(%s) and object(%s)!!\n", input.PartNumber, 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)
    }
}