s3api_bucket.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package s3bucket
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "unicode"
  7. )
  8. // https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
  9. func VerifyS3BucketName(name string) (err error) {
  10. if len(name) < 3 || len(name) > 63 {
  11. return fmt.Errorf("bucket name must between [3, 63] characters")
  12. }
  13. for idx, ch := range name {
  14. if !(unicode.IsLower(ch) || ch == '.' || ch == '-' || unicode.IsNumber(ch)) {
  15. return fmt.Errorf("bucket name can only contain lower case characters, numbers, dots, and hyphens")
  16. }
  17. if idx > 0 && (ch == '.' && name[idx-1] == '.') {
  18. return fmt.Errorf("bucket names must not contain two adjacent periods")
  19. }
  20. //TODO buckets with s3 transfer acceleration cannot have . in name
  21. }
  22. if name[0] == '.' || name[0] == '-' {
  23. return fmt.Errorf("name must start with number or lower case character")
  24. }
  25. if name[len(name)-1] == '.' || name[len(name)-1] == '-' {
  26. return fmt.Errorf("name must end with number or lower case character")
  27. }
  28. if strings.HasPrefix(name, "xn--") {
  29. return fmt.Errorf("prefix xn-- is reserved and not allowed in bucket prefix")
  30. }
  31. if strings.HasSuffix(name, "-s3alias") {
  32. return fmt.Errorf("suffix -s3alias is reserved and not allowed in bucket suffix")
  33. }
  34. if net.ParseIP(name) != nil {
  35. return fmt.Errorf("bucket name cannot be ip addresses")
  36. }
  37. return nil
  38. }