Dynamically adding Captions to images

The media uploader wraps the image and caption in the shortcode.
There is a filter called img_caption_shortcode that can do what you want. The caption itself is passed as part of an array to the shortcode handler, as is the image id.

add_filter( 'img_caption_shortcode', 'wwm_img_caption_filter', 10, 3 );

function wwm_img_caption_filter( $empty, $attr, $content )
{

  $image_id = $attr['id'];
  //do some checking of the $image_id (attachment id) to see what it links to
  //if it links to amazon, include the link in the caption:
  if ( /*my condition is met*/ ) {

   $attr['caption'] = ;//modify the string appropriately
  }

  return ;//some string - see documentation
}

Here’s some documentation on the filter: http://codex.wordpress.org/Plugin_API/Filter_Reference/img_caption_shortcode

UPDATE **

if the image in question doesn’t have a caption already use the_content filter

add_filter( 'the_content', 'add_affiliate_link_caption' );

function add_affiliate_link_caption( $content )
{

 //I'm not even going to try a regex...
 //but some sort of regex find/replace stuff on $content

 return $content;
}