Created
May 7, 2019 08:23
-
-
Save catleeball/954a6749a4c52708fae6980f6edb3892 to your computer and use it in GitHub Desktop.
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
def IsBucketNameValid(bucket_name): | |
"""Check if string meets all bucket name requirements. | |
Requirements for bucket names defined at: | |
https://cloud.google.com/storage/docs/naming | |
Args: | |
Unicode tring, name of bucket. Full name, including provider prefix, i.e.: | |
'gs://my-bucket-name' | |
Returns: | |
Boolean, whether the bucket name is valid or not. | |
""" | |
def _StripAllowedSymbols(s): | |
"""Remove '-', '_', '/', and '.' from given string.""" | |
return ''.join((char for char in s if char not in ['/', '-', '_', '.'])) | |
if not '://' in bucket_name: | |
return False | |
prefix, url = bucket_name.split('://') | |
url = url.rstrip() | |
if url[-1] == '/': | |
url = url[:-1] | |
return all([ | |
prefix.isalpha(), | |
prefix.islower(), | |
len(url) > 2, | |
url[0].isalnum(), | |
url[-1].isalnum(), | |
not url.startswith('goog'), | |
not url.isupper(), | |
_StripAllowedSymbols(url).isalnum() | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment