Last active
September 27, 2020 23:11
-
-
Save Gatsby-Lee/03be30df6ff03b25a5fcf2bcb8609f5f to your computer and use it in GitHub Desktop.
boto3 validates the given endpoint_url by is_valid_endpoint_url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @ref: https://github.com/boto/botocore/blob/develop/botocore/utils.py#L897 | |
import re | |
import sys | |
from botocore.compat import urlsplit | |
def is_valid_endpoint_url(endpoint_url): | |
"""Verify the endpoint_url is valid. | |
:type endpoint_url: string | |
:param endpoint_url: An endpoint_url. Must have at least a scheme | |
and a hostname. | |
:return: True if the endpoint url is valid. False otherwise. | |
""" | |
parts = urlsplit(endpoint_url) | |
hostname = parts.hostname | |
if hostname is None: | |
return False | |
if len(hostname) > 255: | |
return False | |
if hostname[-1] == ".": | |
hostname = hostname[:-1] | |
allowed = re.compile( | |
r"^((?!-)[A-Z\d-]{1,63}(?<!-)\.)*((?!-)[A-Z\d-]{1,63}(?<!-))$", | |
re.IGNORECASE) | |
return allowed.match(hostname) | |
print(sys.argv[1]) | |
print(is_valid_endpoint_url(sys.argv[1])) | |
# boto3 validates the given endpoint_url by is_valid_endpoint_url | |
# s3_client = boto3.client("s3", endpoint_url="http://127.0.0.1:4566") | |
# s3_client.create_bucket(Bucket=bucket_name) | |
# Invalid: endpoint_url="http://s3_emulator:4566" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment