Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle s3.amazonaws.com.cn URL for s3 getter #335

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion get_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (g *S3Getter) parseUrl(u *url.URL) (region, bucket, path, version string, c
// This just check whether we are dealing with S3 or
// any other S3 compliant service. S3 has a predictable
// url as others do not
if strings.Contains(u.Host, "amazonaws.com") {
if strings.HasSuffix(u.Host, "amazonaws.com") {
// Amazon S3 supports both virtual-hosted–style and path-style URLs to access a bucket, although path-style is deprecated
// In both cases few older regions supports dash-style region indication (s3-Region) even if AWS discourages their use.
// The same bucket could be reached with:
Expand Down Expand Up @@ -266,6 +266,32 @@ func (g *S3Getter) parseUrl(u *url.URL) (region, bucket, path, version string, c
}
version = u.Query().Get("version")

} else if strings.HasSuffix(u.Host, "amazonaws.com.cn") {
// S3 buckets in China:
// s3.region.amazonaws.com.cn/bucket/path
// bucket.s3.region.amazonaws.com.cn/path
hostParts := strings.Split(u.Host, ".")
switch len(hostParts) {
case 5:
if hostParts[0] != "s3" {
err = fmt.Errorf("URL is not a valid S3 URL")
return
}
region = hostParts[1]
pathParts := strings.SplitN(u.Path, "/", 3)
bucket = pathParts[1]
path = pathParts[2]
case 6:
region = hostParts[2]
pathParts := strings.SplitN(u.Path, "/", 2)
bucket = hostParts[0]
path = pathParts[1]
default:
err = fmt.Errorf("URL is not a valid S3 URL")
return
}
version = u.Query().Get("version")

} else {
pathParts := strings.SplitN(u.Path, "/", 3)
if len(pathParts) != 3 {
Expand Down
16 changes: 16 additions & 0 deletions get_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ func TestS3Getter_Url(t *testing.T) {
path: "foo/bar.baz",
version: "1234",
},
{
name: "AWSv1234-CN",
url: "s3::https://s3.cn-north-1.amazonaws.com.cn/bucket/foo/bar.baz?version=1234",
region: "cn-north-1",
bucket: "bucket",
path: "foo/bar.baz",
version: "1234",
},
{
name: "AWSVhostDot",
url: "s3::https://bucket.s3.eu-west-1.amazonaws.com/foo/bar.baz?version=1234",
Expand All @@ -197,6 +205,14 @@ func TestS3Getter_Url(t *testing.T) {
path: "foo/bar.baz",
version: "1234",
},
{
name: "AWSVhostDot-CN",
url: "s3::https://bucket.s3.cn-north-1.amazonaws.com.cn/foo/bar.baz?version=1234",
region: "cn-north-1",
bucket: "bucket",
path: "foo/bar.baz",
version: "1234",
},
{
name: "AWSVhostDash",
url: "s3::https://bucket.s3-eu-west-1.amazonaws.com/foo/bar.baz?version=1234",
Expand Down