AWS S3 Bucket Name in GO
Validate AWS S3 bucket names per the standard naming rules: 3–63 chars, lowercase, alphanumeric + dots + hyphens.
Try it in the GO tester →Pattern
regexGO
^[a-z0-9](?:[a-z0-9.\-]{1,61}[a-z0-9])?$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-z0-9](?:[a-z0-9.\-]{1,61}[a-z0-9])?$`)
input := `my-app-uploads`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
First and last char must be lowercase alphanumeric. Middle can contain dots and hyphens too. Length is 3–63 chars total. This is the STRUCTURAL rule — S3 also forbids names that look like IPs and a few other edge cases the regex doesn't catch.
Examples
Input
my-app-uploadsMatches
my-app-uploads
Input
logs.example.comMatches
logs.example.com
Input
Invalid_NameNo match
—