Skip to content

Commit

Permalink
Allow to pass region in the custom s3 storage url like this:
Browse files Browse the repository at this point in the history
s3+http://user:[email protected]:9000/path/to/region-name/bucket-name
  • Loading branch information
darkdarkdragon committed Apr 28, 2021
1 parent 60b297e commit 5153377
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### General

- \#1848 Use fee cut instead of fee share for user facing language in the CLI (@kyriediculous)
- \#1854 Allow to pass region in the custom s3 storage URL (@kdarkdarkdragon)

#### Broadcaster

Expand Down
13 changes: 9 additions & 4 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func ParseOSURL(input string, useFullAPI bool) (OSDriver, error) {
}
if u.Scheme == "s3" {
pw, ok := u.User.Password()
if ok == false {
if !ok {
return nil, fmt.Errorf("password is required with s3:// OS")
}
base := path.Base(u.Path)
Expand All @@ -145,7 +145,12 @@ func ParseOSURL(input string, useFullAPI bool) (OSDriver, error) {
if u.Scheme == "s3+https" {
scheme = "https"
}
_, bucket := path.Split(u.Path)
region := "ignored"
base, bucket := path.Split(u.Path)
if len(base) > 1 && base[len(base)-1] == '/' {
base = base[:len(base)-1]
_, region = path.Split(base)
}
hosturl, err := url.Parse(input)
if err != nil {
return nil, err
Expand All @@ -154,10 +159,10 @@ func ParseOSURL(input string, useFullAPI bool) (OSDriver, error) {
hosturl.Scheme = scheme
hosturl.Path = ""
pw, ok := u.User.Password()
if ok == false {
if !ok {
return nil, fmt.Errorf("password is required with s3:// OS")
}
return NewCustomS3Driver(hosturl.String(), bucket, u.User.Username(), pw, useFullAPI), nil
return NewCustomS3Driver(hosturl.String(), bucket, region, u.User.Username(), pw, useFullAPI), nil
}
if u.Scheme == "gs" {
file := u.User.Username()
Expand Down
16 changes: 15 additions & 1 deletion drivers/drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,28 @@ func TestS3URL(t *testing.T) {

func TestCustomS3URL(t *testing.T) {
assert := assert.New(t)
os, err := ParseOSURL("s3+http://user:[email protected]:9000/path/to/bucket-name", true)
os, err := ParseOSURL("s3+http://user:[email protected]:9000/bucket-name", true)
s3, iss3 := os.(*s3OS)
assert.Equal(true, iss3)
assert.Equal(nil, err)
assert.Equal("http://example.com:9000", s3.host)
assert.Equal("bucket-name", s3.bucket)
assert.Equal("user", s3.awsAccessKeyID)
assert.Equal("password", s3.awsSecretAccessKey)
assert.Equal("ignored", s3.region)
}

func TestCustomS3URLWithRegion(t *testing.T) {
assert := assert.New(t)
os, err := ParseOSURL("s3+http://user:[email protected]:9000/path/to/region-name/bucket-name", true)
s3, iss3 := os.(*s3OS)
assert.Equal(true, iss3)
assert.Equal(nil, err)
assert.Equal("http://example.com:9000", s3.host)
assert.Equal("bucket-name", s3.bucket)
assert.Equal("region-name", s3.region)
assert.Equal("user", s3.awsAccessKeyID)
assert.Equal("password", s3.awsSecretAccessKey)
}

func TestGSURL(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions drivers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ func NewS3Driver(region, bucket, accessKey, accessKeySecret string, useFullAPI b
}

// NewCustomS3Driver for creating S3-compatible stores other than S3 itself
func NewCustomS3Driver(host, bucket, accessKey, accessKeySecret string, useFullAPI bool) OSDriver {
glog.Infof("using custom s3 with url: %s, bucket %s use full API %v", host, bucket, useFullAPI)
func NewCustomS3Driver(host, bucket, region, accessKey, accessKeySecret string, useFullAPI bool) OSDriver {
glog.Infof("using custom s3 with url: %s, bucket %s region %s use full API %v", host, bucket, region, useFullAPI)
os := &s3OS{
host: host,
bucket: bucket,
awsAccessKeyID: accessKey,
awsSecretAccessKey: accessKeySecret,
region: "ignored",
region: region,
useFullAPI: useFullAPI,
}
if !useFullAPI {
Expand Down

0 comments on commit 5153377

Please sign in to comment.