Skip to content

Commit 285e29b

Browse files
authored
Merge pull request #17201 from rifelpet/automated-cherry-pick-of-#17183-origin-release-1.30
Automated cherry pick of #17183: Use SDK's built-in resolver for S3Path.GetHTTPsUrl
2 parents b651e70 + 282eb25 commit 285e29b

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

util/pkg/vfs/s3fs.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,18 @@ func (p *S3Path) GetHTTPsUrl(dualstack bool) (string, error) {
552552
return "", fmt.Errorf("failed to get bucket details for %q: %w", p.String(), err)
553553
}
554554

555-
var url string
556-
if dualstack {
557-
url = fmt.Sprintf("https://s3.dualstack.%s.amazonaws.com/%s/%s", bucketDetails.region, bucketDetails.name, p.Key())
558-
} else {
559-
url = fmt.Sprintf("https://%s.s3.%s.amazonaws.com/%s", bucketDetails.name, bucketDetails.region, p.Key())
555+
resolver := s3.NewDefaultEndpointResolverV2()
556+
endpoint, err := resolver.ResolveEndpoint(ctx, s3.EndpointParameters{
557+
Bucket: aws.String(bucketDetails.name),
558+
Region: aws.String(bucketDetails.region),
559+
UseDualStack: aws.Bool(dualstack),
560+
})
561+
if err != nil {
562+
return "", fmt.Errorf("failed to resolve endpoint for %q: %w", p.String(), err)
560563
}
561-
return strings.TrimSuffix(url, "/"), nil
564+
565+
endpoint.URI.Path = path.Join(endpoint.URI.Path, p.Key())
566+
return endpoint.URI.String(), nil
562567
}
563568

564569
func (p *S3Path) IsBucketPublic(ctx context.Context) (bool, error) {

util/pkg/vfs/s3fs_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,77 @@ func Test_S3Path_Parse(t *testing.T) {
6464
}
6565
}
6666
}
67+
68+
func TestGetHTTPsUrl(t *testing.T) {
69+
grid := []struct {
70+
Path string
71+
Dualstack bool
72+
Region string
73+
ExpectedURL string
74+
}{
75+
{
76+
Path: "s3://bucket",
77+
Region: "us-east-1",
78+
ExpectedURL: "https://bucket.s3.us-east-1.amazonaws.com",
79+
},
80+
{
81+
Path: "s3://bucket.with.forced.path.style/subpath",
82+
Region: "us-east-1",
83+
ExpectedURL: "https://s3.us-east-1.amazonaws.com/bucket.with.forced.path.style/subpath",
84+
},
85+
{
86+
Path: "s3://bucket/path",
87+
Region: "us-east-2",
88+
ExpectedURL: "https://bucket.s3.us-east-2.amazonaws.com/path",
89+
},
90+
{
91+
Path: "s3://bucket2/path/subpath",
92+
Region: "us-east-1",
93+
ExpectedURL: "https://bucket2.s3.us-east-1.amazonaws.com/path/subpath",
94+
},
95+
{
96+
Path: "s3://bucket2-ds/path/subpath",
97+
Dualstack: true,
98+
Region: "us-east-1",
99+
ExpectedURL: "https://bucket2-ds.s3.dualstack.us-east-1.amazonaws.com/path/subpath",
100+
},
101+
{
102+
Path: "s3://bucket2-cn/path/subpath",
103+
Region: "cn-north-1",
104+
ExpectedURL: "https://bucket2-cn.s3.cn-north-1.amazonaws.com.cn/path/subpath",
105+
},
106+
{
107+
Path: "s3://bucket2-cn-ds/path/subpath",
108+
Dualstack: true,
109+
Region: "cn-north-1",
110+
ExpectedURL: "https://bucket2-cn-ds.s3.dualstack.cn-north-1.amazonaws.com.cn/path/subpath",
111+
},
112+
{
113+
Path: "s3://bucket2-gov/path/subpath",
114+
Region: "us-gov-west-1",
115+
ExpectedURL: "https://bucket2-gov.s3.us-gov-west-1.amazonaws.com/path/subpath",
116+
},
117+
{
118+
Path: "s3://bucket2-gov-ds/path/subpath",
119+
Dualstack: true,
120+
Region: "us-gov-west-1",
121+
ExpectedURL: "https://bucket2-gov-ds.s3.dualstack.us-gov-west-1.amazonaws.com/path/subpath",
122+
},
123+
}
124+
for _, g := range grid {
125+
t.Run(g.Path, func(t *testing.T) {
126+
// Must be nonempty in order to force S3_REGION usage
127+
// rather than querying S3 for the region.
128+
t.Setenv("S3_ENDPOINT", "1")
129+
t.Setenv("S3_REGION", g.Region)
130+
s3path, _ := Context.buildS3Path(g.Path)
131+
url, err := s3path.GetHTTPsUrl(g.Dualstack)
132+
if err != nil {
133+
t.Fatalf("unexpected error: %v", err)
134+
}
135+
if url != g.ExpectedURL {
136+
t.Fatalf("expected url: %v vs actual url: %v", g.ExpectedURL, url)
137+
}
138+
})
139+
}
140+
}

0 commit comments

Comments
 (0)