Unfortunately AWS regions and endpoints values don't always comply to st...

...rict rules

The script didn't work for me in all regions, from the following page I found out they not always can be based on region name, like s3-external-1.amazonaws.com instead of s3-us-east-1.amazonaws.com:
http://docs.amazonwebservices.com/general/latest/gr/rande.html