我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用botocore.exceptions.InvalidDNSNameError()。
def fix_s3_host(request, signature_version, region_name, default_endpoint_url='s3.amazonaws.com', **kwargs): """ This handler looks at S3 requests just before they are signed. If there is a bucket name on the path (true for everything except ListAllBuckets) it checks to see if that bucket name conforms to the DNS naming conventions. If it does, it alters the request to use ``virtual hosting`` style addressing rather than ``path-style`` addressing. This allows us to avoid 301 redirects for all bucket names that can be CNAME'd. """ # By default we do not use virtual hosted style addressing when # signed with signature version 4. if signature_version is not botocore.UNSIGNED and \ 's3v4' in signature_version: return elif not _allowed_region(region_name): return try: switch_to_virtual_host_style( request, signature_version, default_endpoint_url) except InvalidDNSNameError as e: bucket_name = e.kwargs['bucket_name'] logger.debug('Not changing URI, bucket is not DNS compatible: %s', bucket_name)
def switch_to_virtual_host_style(request, signature_version, default_endpoint_url=None, **kwargs): """ This is a handler to force virtual host style s3 addressing no matter the signature version (which is taken in consideration for the default case). If the bucket is not DNS compatible an InvalidDNSName is thrown. :param request: A AWSRequest object that is about to be sent. :param signature_version: The signature version to sign with :param default_endpoint_url: The endpoint to use when switching to a virtual style. If None is supplied, the virtual host will be constructed from the url of the request. """ if request.auth_path is not None: # The auth_path has already been applied (this may be a # retried request). We don't need to perform this # customization again. return elif _is_get_bucket_location_request(request): # For the GetBucketLocation response, we should not be using # the virtual host style addressing so we can avoid any sigv4 # issues. logger.debug("Request is GetBucketLocation operation, not checking " "for DNS compatibility.") return parts = urlsplit(request.url) request.auth_path = parts.path path_parts = parts.path.split('/') # Retrieve what the endpoint we will be prepending the bucket name to. if default_endpoint_url is None: default_endpoint_url = parts.netloc if len(path_parts) > 1: bucket_name = path_parts[1] if not bucket_name: # If the bucket name is empty we should not be checking for # dns compatibility. return logger.debug('Checking for DNS compatible bucket for: %s', request.url) if check_dns_name(bucket_name): # If the operation is on a bucket, the auth_path must be # terminated with a '/' character. if len(path_parts) == 2: if request.auth_path[-1] != '/': request.auth_path += '/' path_parts.remove(bucket_name) # At the very least the path must be a '/', such as with the # CreateBucket operation when DNS style is being used. If this # is not used you will get an empty path which is incorrect. path = '/'.join(path_parts) or '/' global_endpoint = default_endpoint_url host = bucket_name + '.' + global_endpoint new_tuple = (parts.scheme, host, path, parts.query, '') new_uri = urlunsplit(new_tuple) request.url = new_uri logger.debug('URI updated to: %s', new_uri) else: raise InvalidDNSNameError(bucket_name=bucket_name)