Updated on 2024-11-13 GMT+08:00

Uploading a Part (SDK for Node.js)

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.

When uploading a part, you must specify its upload ID and part number. You can select any part number between 1 and 10000. A part number uniquely identifies a part and its location in the object you are uploading. If the number of an uploaded part is used to upload a new part, the uploaded part will be overwritten. Whenever you upload a part, OBS returns the ETag header in the response. For each part upload, you must record the part number and the ETag value. These values are required in subsequent requests to complete a multipart upload.

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.
  • Except for the last part uploaded, the other parts range from 0 to 5 GB, and the last part ranges from 100 KB to 5 GB. Part sizes will not be verified during upload because whether an uploaded part is the last one cannot be determined. Their sizes will be verified when being assembled.
  • Each part is numbered from 1 to 10000.
  • The minimum part size supported by an OBS 3.0 bucket is 100 KB, and that supported by an OBS 2.0 bucket is 5 MB. You are advised to perform the multipart upload on OBS 3.0 buckets.

Method

ObsClient.uploadPart(params)

Request Parameters

Table 1 List of request parameters

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

Value range:

The value can contain 3 to 63 characters.

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 of the object 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.

Restrictions:

None

Value range:

The value can contain 1 to 1,024 characters.

Default value:

None

PartNumber

number

Yes

Explanation:

Part number.

Restrictions:

None

Value range:

An integer ranging from 1 to 10000.

Default value:

None

UploadId

string

Yes

Explanation:

Multipart upload ID, which is generated by initiating a multipart upload.

Restrictions:

None

Value range:

The value must contain 32 characters,

for example, 000001648453845DBB78F2340DD460D8.

Default value:

None

ContentMD5

string

No

Explanation:

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

Restrictions:

Base64-encoded, 128-bit MD5 value of the request body.

Value range:

Base64-encoded, 128-bit MD5 value of the request body calculated based on the RFC 1864 standard.

Example: n58IG6hfM7vqI4K0vnWpog==

Default value:

None

Body

string | stream.Readable

No

Explanation:

Data stream of the object to be uploaded.

Restrictions:

The content size in a single upload ranges from 0 to 5 GB.

Value range:

None

Default value:

None

SourceFile

string

No

Explanation:

Source file path of the object to be uploaded

Restrictions:

  • The content size in a single upload ranges from 0 to 5 GB.
  • Body and SourceFile cannot be used together.
  • If both Body and SourceFile are left blank, the size of the uploaded object is 0 bytes.
  • Offset, PartSize, and SourceFile can be used together to specify part of the source file to upload.

Value range:

None

Default value:

None

Offset

number

No

Explanation:

Start offset of a part in the source file.

Restrictions:

Offset, PartSize, and SourceFile can be used together to specify part of the source file to upload.

Value range:

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

Default value:

0

PartSize

number

No

Explanation:

Part size.

Restrictions:

Offset, PartSize, and SourceFile can be used together to specify part of the source file to upload.

Value range:

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

Default value:

102400

SseKms

string

Yes when SSE-KMS is used

Explanation:

SSE-KMS is used for encrypting objects on the server side.

Restrictions:

Currently, only KMS is supported.

Value range:

kms

Default value:

None

SseKmsKey

string

No

Explanation:

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

Restrictions:

Valid value formats are as follows:

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

In the preceding formats:

Value range:

None

Default value:

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

SseC

string

Yes when SSE-C is used

Explanation:

SSE-C is used for encrypting objects on the server side.

Restrictions:

Only AES256 is supported.

Value range:

AES256

Default value:

None

SseCKey

string

Yes when SSE-C is used

Explanation:

Key used for encrypting the object when SSE-C is used

Restrictions:

A Base64-encoded, 256-bit value, for example, K7QkYpBkM5+hca27fsNkUnNVaobncnLht/rCB2o/9Cw=

Value range:

None

Default value:

None

Responses

Table 2 Responses

Type

Description

Table 3

NOTE:

This API returns a Promise response, which requires the Promise or async/await syntax.

Explanation:

Returned results. For details, see Table 3.

Table 3 Response

Parameter

Type

Description

CommonMsg

ICommonMsg

Explanation:

Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 4.

InterfaceResult

Table 5

Explanation:

Results outputted for a successful call. For details, see Table 5.

Restrictions:

This parameter is not included if the value of Status is greater than 300.

Table 4 ICommonMsg

Parameter

Type

Description

Status

number

Explanation:

HTTP status code returned by the OBS server.

Value range:

A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For details, see Status Codes.

Code

string

Explanation:

Error code returned by the OBS server.

Message

string

Explanation:

Error description returned by the OBS server.

HostId

string

Explanation:

Request server ID returned by the OBS server.

RequestId

string

Explanation:

Request ID returned by the OBS server.

Id2

string

Explanation:

Request ID2 returned by the OBS server.

Indicator

string

Explanation:

Error code details returned by the OBS server.

Table 5 UploadPartOutput

Parameter

Type

Description

RequestId

string

Explanation:

Request ID returned by the OBS server

ETag

string

Explanation:

Base64-encoded, 128-bit MD5 value of an object. The ETag is a unique identifier for the object's contents, used to determine if the object has been updated. For example, if the ETag value is A when an object is uploaded and becomes B when the object is downloaded, this indicates the contents of the object were changed. The ETag reflects changes of an object, not of the metadata. Objects created by the upload and copy operations have unique ETags 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.

Code Examples

This example uploads a part to bucket examplebucket in a specified multipart upload (uploadId) of object example/objectname.

 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
51
52
53
54
55
56
// Import the OBS library.
// Use npm to install the client.
const ObsClient = require("esdk-obs-nodejs");
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
const obsClient = new ObsClient({
  // 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.
  access_key_id: process.env.ACCESS_KEY_ID,
  secret_access_key: process.env.SECRET_ACCESS_KEY,
  // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
  // security_token: process.env.SECURITY_TOKEN,
  // Enter the endpoint corresponding to the region where the bucket is located. EU-Dublin is used here in this example. Replace it with the one currently in use.
  server: "https://obs.eu-west-101.myhuaweicloud.eu"
});

async function uploadPart() {
  try {
    const params = {
      // Specify the bucket name.
      Bucket: "examplebucket",
      // Specify the object. example/objectname is used in this example.
      Key: "example/objectname",
      // Specify the ID of the multipart upload.
      UploadId: "0000000xxxxxxxx",
      // Specify the part number (1 in this example) of the part to upload. The part number is an integer ranging from 1 to 10000.
      PartNumber: 1,
      // Specify the source file path of the part to upload (localfile in this example).
      SourceFile: "localfile",
      // Specify the part size (5 MB in this example), in bytes.
      PartSize: 5 * 1024 * 1024,
      // Specify the offset of the part (0 in this example).
      Offset: 0
    };
    // Upload the part.
    const result = await obsClient.uploadPart(params);
    if (result.CommonMsg.Status <= 300) {
      console.log("Upload part(%d) successful with bucket(%s) and object(%s)!", params.PartNumber, params.Bucket, params.Key);
      console.log("RequestId: %s", result.CommonMsg.RequestId);
      console.log("ETag: %s", result.InterfaceResult.ETag);
      return;
    };
    console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
    console.log("Status: %d", result.CommonMsg.Status);
    console.log("Code: %s", result.CommonMsg.Code);
    console.log("Message: %s", result.CommonMsg.Message);
    console.log("RequestId: %s", result.CommonMsg.RequestId);
  } catch (error) {
    console.log("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.");
    console.log(error);
  };
};

uploadPart();