'An S3 request sent to the wrong region will return an error that
contains the endpoint the request should be sent to. This handler
will add the redirect information to the signing context and then
redirect the request.'

def redirect_from_error(self, request_dict, response, operation, **kwargs):

    if (response is None):
        return
    error = response[1].get('Error', {})
    error_code = error.get('Code')
    if (error_code == '301'):
        if (operation.name not in ['HeadObject', 'HeadBucket']):
            return
    elif (error_code != 'PermanentRedirect'):
        return
    bucket = request_dict['context']['signing']['bucket']
    client_region = request_dict['context'].get('client_region')
    new_region = self.get_bucket_region(bucket, response)
    if (new_region is None):
        logger.debug(('S3    client    configured    for    region    %s    but    the    bucket    %s    is    not    in    that    region    and    the    proper    region    could    not    be    automatically    determined.' % (client_region, bucket)))
        return
    logger.debug(('S3    client    configured    for    region    %s    but    the    bucket    %s    is    in    region    %s;    Please    configure    the    proper    region    to    avoid    multiple    unnecessary    redirects    and    signing    attempts.' % (client_region, bucket, new_region)))
    endpoint = self._endpoint_resolver.resolve('s3', new_region)
    endpoint = endpoint['endpoint_url']
    signing_context = {'region': new_region, 'bucket': bucket, 'endpoint': endpoint}
    request_dict['context']['signing'] = signing_context
    self._cache[bucket] = signing_context
    self.set_request_url(request_dict, request_dict['context'])
    return 0
