You can copy an object from a bucket (source bucket) to another bucket (target bucket) in the same region.
Run the following code to copy an object.
// Construct a copy request.
CopyObjectRequest copyObjectRequest = new CopyObjectRequest("<srcBucketName>", "<srcObjectKey>",
"<destBucketName>", "<destObjectKey>");
// (Optional) Configure the metadata of the object to be copied.
// ObjectMetadata objectMetadata = new ObjectMetadata();
// objectMetadata.setContentType("application/octet-stream");
// copyObjectRequest.setNewObjectMetadata(objectMetadata);
// Copy the object by calling the asynchronous interface.
OSSAsyncTask copyTask = oss.asyncCopyObject(copyObjectRequest, new OSSCompletedCallback<CopyObjectRequest, CopyObjectResult>() {
@Override
public void onSuccess(CopyObjectRequest request, CopyObjectResult result) {
Log.d("copyObject", "copy success!");
}
@Override
public void onFailure(CopyObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
// Handle the exceptions returned for the request.
if (clientExcepion != null) {
// A local exception (such as network exception) occurs.
clientExcepion.printStackTrace();
}
if (serviceException != null) {
// A service exception occurs.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Note
- The source object and the target object must be in the same region.
- If the path of the source object is the same as that of the target bucket, you can modify the metadata of the existing target bucket.
- You can only copy objects that is smaller or equal to 1 GB. For objects larger than 1 GB, you must copy them by performing other operations, such as MultipartUpload.