github.com/weiwenhao/getter@v1.30.1/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 if len(hostParts) == 5 && hostParts[1] == "s3" {
    38  		return d.detectNewVhostStyle(hostParts[2], hostParts[0], parts[1:])
    39  	} else {
    40  		return "", false, fmt.Errorf(
    41  			"URL is not a valid S3 URL")
    42  	}
    43  }
    44  
    45  func (d *S3Detector) detectPathStyle(region string, parts []string) (string, bool, error) {
    46  	urlStr := fmt.Sprintf("https://%s.amazonaws.com/%s", region, strings.Join(parts, "/"))
    47  	url, err := url.Parse(urlStr)
    48  	if err != nil {
    49  		return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
    50  	}
    51  
    52  	return "s3::" + url.String(), true, nil
    53  }
    54  
    55  func (d *S3Detector) detectVhostStyle(region, bucket string, parts []string) (string, bool, error) {
    56  	urlStr := fmt.Sprintf("https://%s.amazonaws.com/%s/%s", region, bucket, strings.Join(parts, "/"))
    57  	url, err := url.Parse(urlStr)
    58  	if err != nil {
    59  		return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
    60  	}
    61  
    62  	return "s3::" + url.String(), true, nil
    63  }
    64  
    65  func (d *S3Detector) detectNewVhostStyle(region, bucket string, parts []string) (string, bool, error) {
    66  	urlStr := fmt.Sprintf("https://s3.%s.amazonaws.com/%s/%s", region, bucket, strings.Join(parts, "/"))
    67  	url, err := url.Parse(urlStr)
    68  	if err != nil {
    69  		return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
    70  	}
    71  
    72  	return "s3::" + url.String(), true, nil
    73  }