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
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
input |
Yes |
Explanation: Input parameters for copying a part. For details, see Table 2. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
Bucket |
string |
Yes |
Explanation: Target bucket name Restrictions:
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:
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 |
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 |
No |
Explanation: Server-side decryption header used for decrypting the source object. For details, see Table 3. |
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
Parameter |
Type |
Description |
---|---|---|
output |
type CopyPartOutput struct |
Explanation: Returned results. For details, see Table 5. |
err |
error |
Explanation: Error messages returned by the API |
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 |
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 |
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 |
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:
In the preceding formats:
Default value:
|
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) } } |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot