Skip to content

Commit da47b87

Browse files
authored
Allow specifying S3 region in url as "s3://<bucket>.s3.<region>.amazo… (#427)
Allow specifying S3 region in url as "s3://<bucket>.s3.<region>.amazonaws.com"
1 parent 2d9233a commit da47b87

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

request/s3.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package request
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"io"
78
"net/url"
89
"strings"
@@ -31,10 +32,20 @@ func NewS3FromUrl(ustr string) (*S3, error) {
3132
if err != nil {
3233
return nil, err
3334
}
35+
bucket := strings.TrimPrefix(u.Host, "s3://")
36+
37+
// Is bucket in the form <bucket>.s3.<region>.amazonaws.com?
38+
bucketRegion := ""
39+
if a := strings.Split(bucket, "."); len(a) == 5 && a[1] == "s3" {
40+
bucket = a[0]
41+
bucketRegion = a[2]
42+
}
3443
s := S3{
35-
Bucket: trimSlash(strings.TrimPrefix(u.Host, "s3://")),
44+
Bucket: trimSlash(bucket),
3645
KeyPrefix: trimSlash(u.Path),
3746
}
47+
s.secret.AWSRegion = bucketRegion
48+
fmt.Printf("s: %#v\n", s)
3849
return &s, nil
3950
}
4051

request/s3_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ func TestS3(t *testing.T) {
1919
}
2020
testBucket(t, ctx, b)
2121
}
22+
23+
func TestAsd(t *testing.T) {
24+
s, _ := NewS3FromUrl("s3://asd.s3.us-west-2.amazonaws.com/asd123")
25+
_ = s
26+
}

0 commit comments

Comments
 (0)