github.com/remind101/go-getter@v0.0.0-20180809191950-4bda8fa99001/detect_s3.go (about) 1 package getter 2 3 import ( 4 "fmt" 5 "net/url" 6 "strings" 7 ) 8 9 // S3Detector implements Detector to detect S3 URLs and turn 10 // them into URLs that the S3 getter can understand. 11 type S3Detector struct{} 12 13 func (d *S3Detector) Detect(src, _ string) (string, bool, error) { 14 if len(src) == 0 { 15 return "", false, nil 16 } 17 18 if strings.Contains(src, ".amazonaws.com/") { 19 return d.detectHTTP(src) 20 } 21 22 return "", false, nil 23 } 24 25 func (d *S3Detector) detectHTTP(src string) (string, bool, error) { 26 parts := strings.Split(src, "/") 27 if len(parts) < 2 { 28 return "", false, fmt.Errorf( 29 "URL is not a valid S3 URL") 30 } 31 32 hostParts := strings.Split(parts[0], ".") 33 if len(hostParts) == 3 { 34 return d.detectPathStyle(hostParts[0], parts[1:]) 35 } else if len(hostParts) == 4 { 36 return d.detectVhostStyle(hostParts[1], hostParts[0], parts[1:]) 37 } else { 38 return "", false, fmt.Errorf( 39 "URL is not a valid S3 URL") 40 } 41 } 42 43 func (d *S3Detector) detectPathStyle(region string, parts []string) (string, bool, error) { 44 urlStr := fmt.Sprintf("https://%s.amazonaws.com/%s", region, strings.Join(parts, "/")) 45 url, err := url.Parse(urlStr) 46 if err != nil { 47 return "", false, fmt.Errorf("error parsing S3 URL: %s", err) 48 } 49 50 return "s3::" + url.String(), true, nil 51 } 52 53 func (d *S3Detector) detectVhostStyle(region, bucket string, parts []string) (string, bool, error) { 54 urlStr := fmt.Sprintf("https://%s.amazonaws.com/%s/%s", region, bucket, strings.Join(parts, "/")) 55 url, err := url.Parse(urlStr) 56 if err != nil { 57 return "", false, fmt.Errorf("error parsing S3 URL: %s", err) 58 } 59 60 return "s3::" + url.String(), true, nil 61 }