AWS S3 Bucket Name Validation Regex

Amazon Web Services enforces a strict naming convention for buckets used for storing files. Amazon’s requirements for bucket names include: aws s3 bucket name validation regex by Scott Lanoue at Easy Dynamics Technical Blog

  • A Bucket’s name can be between 6 and 63 characters long, containing lowercase characters, numbers, periods, and dashes
  • Each label must start with a lowercase letter or number
  • Bucket names cannot contain underscores, end with a dash, have consecutive periods, or use dashes adjacent to periods
  • Lastly, the bucket name cannot be formatted as an IPV4 address (e.g. 255.255.255.255)

Enforcing Naming Requirements 

In order to enforce these naming requirements, I have created a regular expression that satisfies all of the conditions outlined about:

                ^([a-z]|(d(?!d{0,2}.d{1,3}.d{1,3}.d{1,3})))([a-zd]|(.(?!(.|-)))|(-(?!.))){1,61}[a-zd.]$

I was fortunate enough to be able to use Regular Expressions 101 to allow us to test and follow through our regular expression, here is the explanation of our regex:

^([az]|(d(?!d{0,2}.d{1,3}.d{1,3}.d{1,3})))([azd]|(.(?!(.|)))|((?!.))){1,61}[azd.]$

·         ^ assert position at start of a line

·         1st Capturing group ([a-z]|(d(?!d{0,2}.d{1,3}.d{1,3}.d{1,3})))

o    1st Alternative: [a-z]

§  [a-z] match a single character present in the list below

§  a-z a single character in the range between a and z (case sensitive)

o    2nd Alternative: (d(?!d{0,2}.d{1,3}.d{1,3}.d{1,3}))

§  2nd Capturing group (d(?!d{0,2}.d{1,3}.d{1,3}.d{1,3}))

§  d match a digit [0-9]

§  (?!d{0,2}.d{1,3}.d{1,3}.d{1,3}) Negative Lookahead – Assert that it is impossible to match the regex below

§  d{0,2} match a digit [0-9]

§  Quantifier: {0,2} Between 0 and 2 times, as many times as possible, giving back as needed [greedy]

§  . matches the character . literally

§  d{1,3} match a digit [0-9]

§  Quantifier: {1,3} Between 1 and 3 times, as many times as possible, giving back as needed [greedy]

§  . matches the character . literally

§  d{1,3} match a digit [0-9]

§  Quantifier: {1,3} Between 1 and 3 times, as many times as possible, giving back as needed [greedy]

§  . matches the character . literally

§  d{1,3} match a digit [0-9]

§  Quantifier: {1,3} Between 1 and 3 times, as many times as possible, giving back as needed [greedy]

·         3rd Capturing group ([a-zd]|(.(?!(.|-)))|(-(?!.))){1,61}

o    Quantifier: {1,61} Between 1 and 61 times, as many times as possible, giving back as needed [greedy]

o    Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you’re not interested in the data

o    1st Alternative: [a-zd]

§  [a-zd] match a single character present in the list below

§  a-z a single character in the range between a and z (case sensitive)

§  d match a digit [0-9]

o    2nd Alternative: (.(?!(.|-)))

§  4th Capturing group (.(?!(.|-)))

§  . matches the character . literally

§  (?!(.|-)) Negative Lookahead – Assert that it is impossible to match the regex below

§  5th Capturing group (.|-)

§  1st Alternative: .

§  . matches the character . literally

§  2nd Alternative: 

§   matches the character  literally

o    3rd Alternative: (-(?!.))

§  6th Capturing group (-(?!.))

§   matches the character  literally

§  (?!.) Negative Lookahead – Assert that it is impossible to match the regex below

§  . matches the character . literally

·         [a-zd.] match a single character present in the list below

o    a-z a single character in the range between a and z (case sensitive)

o    d match a digit [0-9]

o    . matches the character . literally

·         $ assert position at end of a line

Test Cases

Additionally, I wrote test cases trying certain patterns against the AWS S3 Bucket Name Requirements Page:

Passes

Fails

bucketname

hyphen-.dottest

nam

dot.-hyphentest

77b69873-6e45-40df-ae2a-3e4323d2f468

dot..dottest

594a1613-a272-4de5-8ef9-25f5c90035c4

192.168.5.4

Thank you to Pranav Kothare for helping me to develop, fine tune, and test this regular expression. Any feedback would be awesome!


What other Regex’s do you use to satisfy all the conditions? Share your wisdom with us in a comment below!

Found this blog post useful? Make yourself comfortable and check out our blog home page to explore other technologies we use on a daily basis and the fixes we’ve solved in our day to day work. To make your life easier, subscribe to our blog to get instant updates sent straight to your inbox:

{{cta(‘33985992-7ced-4ebd-b7c8-4fcb88ae3da4′,’justifycenter’)}}

Leave a Comment

Easy Dynamics Login