1package getter 2 3import ( 4 "fmt" 5 "net/url" 6 "strings" 7) 8 9// GCSDetector implements Detector to detect GCS URLs and turn 10// them into URLs that the GCSGetter can understand. 11type GCSDetector struct{} 12 13func (d *GCSDetector) Detect(src, _ string) (string, bool, error) { 14 if len(src) == 0 { 15 return "", false, nil 16 } 17 18 if strings.Contains(src, "googleapis.com/") { 19 return d.detectHTTP(src) 20 } 21 22 return "", false, nil 23} 24 25func (d *GCSDetector) detectHTTP(src string) (string, bool, error) { 26 27 parts := strings.Split(src, "/") 28 if len(parts) < 5 { 29 return "", false, fmt.Errorf( 30 "URL is not a valid GCS URL") 31 } 32 version := parts[2] 33 bucket := parts[3] 34 object := strings.Join(parts[4:], "/") 35 36 url, err := url.Parse(fmt.Sprintf("https://www.googleapis.com/storage/%s/%s/%s", 37 version, bucket, object)) 38 if err != nil { 39 return "", false, fmt.Errorf("error parsing GCS URL: %s", err) 40 } 41 42 return "gcs::" + url.String(), true, nil 43} 44