def get_target_url(endpoint_url, bucket_name=None, object_name=None,
                   bucket_region='us-east-1', query=None):
    """
    Construct final target url.

    :param endpoint_url: Target endpoint url where request is served to.
    :param bucket_name: Bucket component for the target url.
    :param object_name: Object component for the target url.
    :param bucket_region: Bucket region for the target url.
    :param query: Query parameters as a *dict* for the target url.
    :return: Returns final target url as *str*.
    """
    # New url
    url = None

    # Parse url
    parsed_url = urlsplit(endpoint_url)

    # Get new host, scheme.
    scheme = parsed_url.scheme
    host = parsed_url.netloc

    # Strip 80/443 ports since curl & browsers do not
    # send them in Host header.
    if (scheme == 'http' and parsed_url.port == 80) or\
       (scheme == 'https' and parsed_url.port == 443):
        host = parsed_url.hostname

    if 's3.amazonaws.com' in host:
        host = get_s3_endpoint(bucket_region)

    url = scheme + '://' + host
    if bucket_name:
        # Save if target url will have buckets which suppport
        # virtual host.
        is_virtual_host_style = is_virtual_host(endpoint_url,
                                                bucket_name)
        if is_virtual_host_style:
            url = (scheme + '://' + bucket_name + '.' + host)
        else:
            url = (scheme + '://' + host + '/' + bucket_name)

    url_components = [url]
    url_components.append('/')

    if object_name:
        object_name = encode_object_name(object_name)
        url_components.append(object_name)

    if query:
        ordered_query = collections.OrderedDict(sorted(query.items()))
        query_components = []
        for component_key in ordered_query:
            if isinstance(ordered_query[component_key], list):
                for value in ordered_query[component_key]:
                    query_components.append(component_key+'='+
                                            queryencode(value))
            else:
                query_components.append(
                    component_key+'='+
                    queryencode(ordered_query.get(component_key, '')))

        query_string = '&'.join(query_components)
        if query_string:
            url_components.append('?')
            url_components.append(query_string)

    return ''.join(url_components)
Construct final target url.

    :param endpoint_url: Target endpoint url where request is served to.
    :param bucket_name: Bucket component for the target url.
    :param object_name: Object component for the target url.
    :param bucket_region: Bucket region for the target url.
    :param query: Query parameters as a *dict* for the target url.
    :return: Returns final target url as *str*.