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

Copying a Part (SDK for Node.js)

If you have any questions during development, post them on the Issues page of GitHub.

Function

This API uploads a part for a specified multipart upload by copying data to a specified bucket.

After initiating a multipart upload task, you can add parts for this task by copying part or all of an existing object in OBS.

Assume that you copy a source object and save it as part1. If there is already an existing part called part1, the new part1 will overwrite the existing part1. Then, only the new part1 can be listed. The old part1 is deleted. To prevent accidental deletion, ensure there is no existing object with the same name as the part involved when using this API. The source object remains unchanged during the copy process.

Restrictions

  • To copy 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.
  • You cannot determine whether the operation is successful solely based on status_code returned in the HTTP header. For example, code 200 indicates that the server has received and started to process the request. The copy operation is considered successful only when the response body contains an ETag.

Method

ObsClient.copyPart(params)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

Yes

Explanation:

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

Value range: 1–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

CopySource

string

Yes

Explanation:

Names of the source bucket and object. If the source object has multiple versions, the versionId parameter is used to specify the desired version.

Example: source_bucket/sourceObject?versionId=G001117FCE89978B0000401205D5DC9A

Restrictions:

Full-width characters and percent signs (%) must be URL-encoded.

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 CopyPartOutput

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.

LastModified

string

Explanation:

Time when a part was last modified, in UTC

PartNumber

number

Explanation:

Part number.

SseKms

string

Explanation:

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

SseKmsKey

string

Explanation:

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

SseC

string

Explanation:

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

SseCKeyMd5

string

Explanation:

MD5 value of the key used 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:

Base64-encoded MD5 value of the key, for example, 4XvB3tbNTN+tIEVa0/fGaQ==

Code Examples

This example copies a part from object example/sourceobjectname in bucket sourcebucketname to object example/objectname in 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
51
// 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 copyPart() {
  try {
    const params = {
      // Specify the bucket name.
      Bucket: "examplebucket",
      // Specify an object. example/objectname is used in this example.
      Key: "example/objectname",
      // Specify the names of the source bucket (sourcebucketname in this example) and source object (example/sourceobjectname in this example).
      CopySource: "sourcebucketname/example/sourceobjectname",
      // Specify the part number (1 in this example).
      PartNumber: 1,
      // Specify the multipart upload ID (00000188677110424014075CC4A77xxx in this example).
      UploadId: "00000188677110424014075CC4A77xxx",
    };
    // Copy the part.
    const result = await obsClient.copyPart(params);
    if (result.CommonMsg.Status <= 300) {
      console.log("Complete multipart upload successful with bucket(%s) and object(%s)!", params.Bucket, params.Key);
      console.log("RequestId: %s", result.CommonMsg.RequestId);
      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);
  };
};

copyPart();