Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass region in the custom s3 storage url #1854

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (@darkdarkdragon)

#### 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:password@example.com:9000/path/to/bucket-name", true)
os, err := ParseOSURL("s3+http://user:password@example.com: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:password@example.com:9000/path/to/region-name/bucket-name", true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to /path/to? There's no assert.Equal that has it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ignored. It was always ignored (hosturl.Path = "")

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