Update:
I managed to figure out what the problem was and fixed it. I added the following code to function upload_images():
$check_original_image = false;
for($i=0;$i<count($files_to_upload['upload']);$i++){
if($files_to_upload['upload'][$i]['fn']==$meta_value['file']){
$check_original_image = true;
}
}
if(!$check_original_image){
$files_to_upload['upload'][] = array('fn' => $meta_value['file']);
}
Now the original file gets uploaded to CDN, and url is available:
https://xxxxxxxxx.cf2.rackcdn.com/filename.jpg
but the url of the image is still local,
https://mysite.com/wp-content/uploads/filename.jpg
Because I chose to remove local image once it’s uploaded to cdn, the image still can’t be displayed.
I am not sure how to resolve this kind of problem.