Copy Object
Users can copy an object using the copyObject method, as shown in the following code:
1bos_client.copy_object(source_bucket_name, source_object_key, target_bucket_name, target_object_key)
Synchronous copy function
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.
If the source object is an archive-class file, you must first restore it before performing the operation.
If you're using an SDK version older than bce-python-sdk-0.8.12, there could be cases where the copy request succeeds, but the file copy itself fails. To avoid such issues, it is recommended to use the latest version of the SDK.
Multipart copy
Besides the CopyObject API for file copying, BOS also provides another method called Multipart Upload Copy. If the source object is an archive-class file, you must first restore it.
Users can use Multipart Upload Copy in 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.
Copy part code reference (parameter definitions can refer to multipart upload):
1left_size = int(bos_client.get_object_meta_data(source_bucket,source_key).metadata.content_length)
2 #Set the starting position of the part to left_size
3 #Set the starting offset position of the part
4offset = 0
5part_number = 1
6part_list = []
7while left_size > 0:
8 #Set each part to 5MB
9 part_size = 5 * 1024 * 1024
10 if left_size < part_size:
11 part_size = left_size
12 response = bos_client.upload_part_copy(source_bucket, source_key, target_bucket, target_key, upload_id,part_number, part_size, offset)
13 left_size -= part_size
14 offset += part_size
15 part_list.append({
16 "partNumber": part_number,
17 "eTag": response.etag
18 })
19 part_number += 1
Note:
- The offset parameter is specified in bytes and represents the starting position of the part within the file.
- 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。
