From d285c2b223ad3f9e5e1a66d1309b79bf4a3f3368 Mon Sep 17 00:00:00 2001 From: Balazs Nagy Date: Thu, 19 Aug 2021 08:26:27 +0200 Subject: [PATCH] handle s3.amazonaws.com.cn URL for s3 getter --- get_s3.go | 28 +++++++++++++++++++++++++++- get_s3_test.go | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/get_s3.go b/get_s3.go index ec864428e..c7328146d 100644 --- a/get_s3.go +++ b/get_s3.go @@ -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: @@ -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 { diff --git a/get_s3_test.go b/get_s3_test.go index 25ce48367..57ac5849a 100644 --- a/get_s3_test.go +++ b/get_s3_test.go @@ -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", @@ -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",