Skip to content

Commit b4a96a9

Browse files
committed
Fail fast when no AWS region is resolved and use it for S3 object URLs
The switch to resolving AWS credentials/region from the SDK default chain left the region passed to the S3 service empty. Virtual-host style object URLs embed the region in the hostname, so stored archive URLs came out as https://<bucket>.s3..amazonaws.com/... which doesn't resolve. Resolve the region from the SDK chain, error if it's empty, and pass it to the S3 service.
1 parent d0a94f7 commit b4a96a9

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

archives/s3.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/aws/aws-sdk-go-v2/aws"
15+
"github.com/aws/aws-sdk-go-v2/config"
1516
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
1617
"github.com/aws/aws-sdk-go-v2/service/s3"
1718
"github.com/aws/aws-sdk-go-v2/service/s3/types"
@@ -30,7 +31,20 @@ const chunkSizeBytes = 1e9 // 1GB
3031

3132
// NewS3Client creates a new s3 service from the passed in config, testing it as necessary
3233
func NewS3Client(cfg *runtime.Config, test bool) (*s3x.Service, error) {
33-
svc, err := s3x.NewService(context.TODO(), "", cfg.S3Endpoint, cfg.S3PathStyle)
34+
ctx := context.TODO()
35+
36+
// resolve the AWS region from the standard SDK default chain (AWS_REGION / AWS_DEFAULT_REGION env
37+
// vars, shared config, etc.). The SDK doesn't error when no region can be resolved, it just leaves
38+
// it empty, so fail fast here rather than let an empty region break virtual-host style S3 URLs.
39+
awsCfg, err := config.LoadDefaultConfig(ctx)
40+
if err != nil {
41+
return nil, fmt.Errorf("error resolving AWS config: %w", err)
42+
}
43+
if awsCfg.Region == "" {
44+
return nil, fmt.Errorf("no AWS region resolved - set AWS_REGION or AWS_DEFAULT_REGION")
45+
}
46+
47+
svc, err := s3x.NewService(ctx, awsCfg.Region, cfg.S3Endpoint, cfg.S3PathStyle)
3448
if err != nil {
3549
return nil, err
3650
}

0 commit comments

Comments
 (0)