Copy Object
Copy a file
Users can copy an object using the copyObject method, as shown in the following code:
1client.copy_object(source_bucket_name, source_object_key, target_bucket_name, target_object_key)
The copy_object method can be used to configure optional parameters through options; the parameter list is as follows:
| Parameters | Description |
|---|---|
| user-metadata | User-defined Meta, including Key-Value pairs |
| eTag | If the eTag of the source object is specified for upload, it will be compared with the eTag of the target object. If they do not match, an error will occur. |
Synchronous copy
Currently, BOS’s CopyObject API operates in a synchronous manner. This means BOS waits until the copy operation is fully completed before returning a success status. While synchronous copying provides a more accurate status for users, it also takes longer and is proportional to the file size.
The synchronous copy approach aligns better with industry standards and improves compatibility with other platforms. It also simplifies the server’s business logic and enhances service efficiency.
Multipart copy
Apart from using the CopyObject API for copying, BOS offers another method—Multipart Upload Copy. This method is suitable for the following application scenarios (but not limited to these):
- When a resumable copy process is required.
- For copying files larger than 5GB.
- When the connection to the BOS server frequently disconnects due to poor network conditions.
The following section introduces the step-by-step process for implementing the three-step copy method.
The three-step copy includes three steps: init, “copy part”, and complete. Among them, the operations of init and complete are the same as those in multipart upload. You can directly refer to Initialize Multipart Upload and [Complete Multipart Upload](BOS/SDK/Ruby-SDK/File management/Upload files.md#Complete multipart upload).
1left_size = client.get_object_meta_data(source_bucket_name, source_object_key)['content-length']
2offset = 0
3part_number = 1
4part_list = []
5while left_size > 0 do
6 part_size = 5 * 1024 * 1024
7 if left_size < part_size
8 part_size = left_size
9 end
10 response = client.upload_part_copy(
11 source_bucket_name, source_object_key, target_bucket_name, target_object_key, upload_id, part_number, part_size, offset)
12 left_size -= part_size
13 offset += part_size
14 # your should store every part number and etag to invoke complete multi-upload
15 part_list << {
16 "partNumber" => part_number,
17 "eTag" => response["eTag"]
18 }
19 part_number += 1
20end
Note: The size parameter is in bytes and defines the size of each part. Except for the last part, the size of other parts must be larger than 5MB.
