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

Copying a Part (SDK for Go)

Function

This API copies a part to a specified bucket. This request must contain the multipart upload ID.

After creating a multipart upload, you can specify the upload ID and upload parts for the multipart upload. Alternatively, you can call this API to add a part (part of an object or the whole object).

Assume that you copy a source object and save it as part1. If there is already a part1 before the copy, the new part1 will overwrite the existing part1. Then, only the new part1 can be listed and the old part1 is deleted. Therefore, when using this API, ensure that the target part does not exist or is no longer used, to avoid accidental deletion. The source object keeps unchanged in the entire 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.
  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • You cannot determine whether a part copy is successful only based on status_code in the HTTP header returned. A 200 response indicates that the server has received the request and starts to process the request. Only when the response body contains ETag, the copy is successful, or the copy fails.

Method

func (obsClient ObsClient) CopyPart(input *CopyPartInput) (output *CopyPartOutput, err error)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

input

*CopyPartInput

Yes

Explanation:

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

Table 2 CopyPartInput

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

Yes

Explanation:

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

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, for example, 000001648453845DBB78F2340DD460D8

Value range:

The value must contain 32 characters.

Default value:

None

CopySourceBucket

string

Yes

Explanation:

Source 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

CopySourceKey

string

Yes

Explanation:

Source 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

CopySourceRangeStart

int64

No

Explanation:

Start position from which a part copy begins

Value range:

A non-negative integer, in bytes

Default value:

0

CopySourceRangeEnd

int64

No

Explanation:

End position at which a part copy ends

Restrictions:

The value must be greater than that of RangeStart. The upper limit of the value is the object length minus 1, in bytes.

Value range:

A non-negative integer, in bytes

Default value:

None

SseHeader

SseCHeader

No

Explanation:

Server-side encryption header used for encrypting the target object. For details, see Table 3.

Restrictions:

If the object uploaded to a server is encrypted with the key provided by the client, the key must also be provided in the message for downloading the object.

SourceSseHeader

SseCHeader

No

Explanation:

Server-side decryption header used for decrypting the source object. For details, see Table 3.

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

*CopyPartOutput

type CopyPartOutput struct

Explanation:

Returned results. For details, see Table 5.

err

error

Explanation:

Error messages returned by the API

Table 5 CopyPartOutput

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:

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

LastModified

time.Time

Explanation:

Time when the last modification was made to the target object

Restrictions:

The time must be in the ISO8601 format, for example, 2018-01-01T00:00:00.000Z.

Sample code: time.Now().Add(time.Duration(24) * time.Hour)

Default value:

None

SseHeader

SseCHeader or SseKmsHeader

struct SseCHeader type
type SseKmsHeader struct

Explanation:

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

PartNumber

int

Explanation:

Part number

Value range:

An integer ranging from 1 to 10000

Default value:

None

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:

Default value:

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

Code Example

This example copies parts from one bucket to another.

 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/intl/en-us/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. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"
    // 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.CopyPartInput{}
    // Specify a bucket name.
    input.Bucket = "examplebucket"
    // Specify an object (example/objectname as an example).
    input.Key = "example/objectname"
    // Specify a source bucket (sourcebucketname as an example).
    input.CopySourceBucket = "sourcebucketname"
    // Specify an object to copy (example/sourceobjectname as an example) in the source bucket.
    input.CopySourceKey = "example/sourceobjectname"
    // Specify the number (1 as an example) of the part to upload.
    input.PartNumber = 1
    // Copy the part.
    output, err := obsClient.CopyPart(input)
    if err == nil {
        fmt.Printf("Copy part(bucket:%s, object: %s) successful from bucket:%s's object: %s!\n", 
             input.Bucket, input.Key, input.CopySourceBucket, input.CopySourceKey)
        fmt.Printf("ETag:%s, LastModified:%s\n", output.ETag, output.LastModified)
        return
    }
    fmt.Printf("Copy part(bucket:%s, object: %s) fail from bucket:%s's object: %s!\n", 
             input.Bucket, input.Key, input.CopySourceBucket, input.CopySourceKey)
    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)
    }
}