Skip to content

Commit

Permalink
Add support to detect 5 parts s3 virtual hosted-style requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdong66 committed Mar 30, 2021
1 parent 8724413 commit e704f62
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions detect_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (d *S3Detector) detectHTTP(src string) (string, bool, error) {
return d.detectPathStyle(hostParts[0], parts[1:])
} else if len(hostParts) == 4 {
return d.detectVhostStyle(hostParts[1], hostParts[0], parts[1:])
} else if len(hostParts) == 5 && hostParts[1] == "s3" {
return d.detectNewVhostStyle(hostParts[2], hostParts[0], parts[1:])
} else {
return "", false, fmt.Errorf(
"URL is not a valid S3 URL")
Expand All @@ -59,3 +61,13 @@ func (d *S3Detector) detectVhostStyle(region, bucket string, parts []string) (st

return "s3::" + url.String(), true, nil
}

func (d *S3Detector) detectNewVhostStyle(region, bucket string, parts []string) (string, bool, error) {
urlStr := fmt.Sprintf("https://s3.%s.amazonaws.com/%s/%s", region, bucket, strings.Join(parts, "/"))
url, err := url.Parse(urlStr)
if err != nil {
return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
}

return "s3::" + url.String(), true, nil
}
5 changes: 5 additions & 0 deletions detect_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func TestS3Detector(t *testing.T) {
"bucket.s3-eu-west-1.amazonaws.com/foo/bar.baz",
"s3::https://s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz",
},
// 5 parts Virtual hosted-style
{
"bucket.s3.eu-west-1.amazonaws.com/foo/bar.baz",
"s3::https://s3.eu-west-1.amazonaws.com/bucket/foo/bar.baz",
},
// Path style
{
"s3.amazonaws.com/bucket/foo",
Expand Down

0 comments on commit e704f62

Please sign in to comment.