Copy Object
Updated at:2025-11-03
Simply Copy Object
Basic workflow
- Instantiate the BOSClient class.
- Call the BOSClient.copyObject() method.
- The method returns a CopyObjectResponse instance. The eTag and last modification time can be obtained through getETag() and getLastModified().
Example code
Java
1// Copy Object
2 CopyObjectResponse copyObjectResponse = client.copyObject(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>); //SrcBucketName, SrcKey are the source addresses,DestBucketName, DestKey are the destination addresses to which the copy is made
3 // Print results
4System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified());
Complete example
Java
1import android.app.Activity;
2import android.os.Bundle;
3import com.baidubce.BceClientException;
4import com.baidubce.BceServiceException;
5import com.baidubce.auth.DefaultBceCredentials;
6import com.baidubce.demo.R;
7import com.baidubce.services.bos.BosClient;
8import com.baidubce.services.bos.BosClientConfiguration;
9import com.baidubce.services.bos.model.CopyObjectResponse;
10public class ExampleActivity extends Activity {
11private String srcBucketName = <SrcBucketName>;
12private String srcKey = <SrcKey>;
13private String destBucketName = <DestBucketName>;
14private String destKey = <DestKey>;
15@Override
16protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19 new Thread(new Runnable() {
20 @Override
21 public void run() {
22 try {
23 BosClientConfiguration config = new BosClientConfiguration();
24 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
25 config.setEndpoint(<EndPoint>);
26 BosClient client = new BosClient(config);
27 // Copy Object
28 CopyObjectResponse copyObjectResponse = client.copyObject(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>);
29 // Print results
30 System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified());
31 } catch (BceServiceException e) {
32 System.out.println("Error ErrorCode: " + e.getErrorCode());
33 System.out.println("Error RequestId: " + e.getRequestId());
34 System.out.println("Error StatusCode: " + e.getStatusCode());
35 System.out.println("Error Message: " + e.getMessage());
36 System.out.println("Error ErrorType: " + e.getErrorType());
37 } catch (BceClientException e) {
38 System.out.println("Error Message: " + e.getMessage());
39 }
40 }
41 }).start();
42}}
Note: The copyObject method returns a
CopyObjectResponseobject containing the new object's ETag and modification time.
Copy Object via CopyObjectRequest
You can also copy object via CopyObjectRequest This function is typically used in the following scenarios:
- Copy an object while resetting its metadata.
- Reset the metadata of an existing object (set the source and destination to the same object).
Basic workflow
- Create an instance of the CopyObjectRequest class, and pass in the parameters
<SrcBucketName>,<SrcKey>,<DestBucketName>,<DestKey>. - Create an ObjectMetadata instance.
- The method returns a CopyObjectResponse instance. The eTag and last modification time can be obtained through getETag() and getLastModified().
Example code
Java
1// Create CopyObjectRequest object
2CopyObjectRequest copyObjectRequest = new CopyObjectRequest(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>);
3 // Set new Metadata
4Map<String, String> userMetadata = new HashMap<String, String>();
5userMetadata.put(<UserMetaKey>,<UserMetaValue>);
6meta.setUserMetadata(userMetadata);
7copyObjectRequest.setNewObjectMetadata(meta);
8 // Copy Object
9CopyObjectResponse copyObjectResponse = client.copyObject(copyObjectRequest);
10System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified());
Note:
CopyObjectRequestallows users to modify the ObjectMeta of the target object and also provides the setting of theMatchingETagConstraintsparameter.
