9 // S3Detector implements Detector to detect S3 URLs and turn
10 // them into URLs that the S3 getter can understand.
11 type S3Detector struct{}
13 func (d *S3Detector) Detect(src, _ string) (string, bool, error) {
18 if strings.Contains(src, ".amazonaws.com/") {
19 return d.detectHTTP(src)
25 func (d *S3Detector) detectHTTP(src string) (string, bool, error) {
26 parts := strings.Split(src, "/")
28 return "", false, fmt.Errorf(
29 "URL is not a valid S3 URL")
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:])
38 return "", false, fmt.Errorf(
39 "URL is not a valid S3 URL")
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)
47 return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
50 return "s3::" + url.String(), true, nil
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)
57 return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
60 return "s3::" + url.String(), true, nil