Skip to content

Instantly share code, notes, and snippets.

@catleeball
Created May 7, 2019 08:23
Show Gist options
  • Save catleeball/954a6749a4c52708fae6980f6edb3892 to your computer and use it in GitHub Desktop.
Save catleeball/954a6749a4c52708fae6980f6edb3892 to your computer and use it in GitHub Desktop.
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