Skip to content

Commit

Permalink
Fix S3 support for Filebase (#68)
Browse files Browse the repository at this point in the history
Filebase requires setting "region" to "us-east-1" in the AWS Client configuration.
  • Loading branch information
leszko authored Jul 7, 2023
1 parent e131f40 commit b4d9ced
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
11 changes: 6 additions & 5 deletions drivers/drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestCustomS3URL(t *testing.T) {
assert.Equal("bucket-name", s3.bucket)
assert.Equal("user", s3.awsAccessKeyID)
assert.Equal("password", s3.awsSecretAccessKey)
assert.Equal("example", s3.region)
assert.Equal("us-east-1", s3.region)
}

func TestCustomS3URLWithRegion(t *testing.T) {
Expand All @@ -82,20 +82,21 @@ func TestCustomS3URLWithRegion(t *testing.T) {
assert.Equal(nil, err)
assert.Equal("http://example.com:9000", s3.host)
assert.Equal("bucket-name", s3.bucket)
assert.Equal("example", s3.region)
assert.Equal("us-east-1", s3.region)
assert.Equal("user", s3.awsAccessKeyID)
assert.Equal("password", s3.awsSecretAccessKey)
}

func TestCustomS3RegionParser(t *testing.T) {
cases := [][]string{
{"http://one:9000", "one"},
{"http://one.two:9000", "one"},
{"http://one.two.three:9000", "two"},
{"http://one:9000", "us-east-1"},
{"http://one.two:9000", "us-east-1"},
{"http://one.two.three:9000", "us-east-1"},
{"http://one.two.three.four:9000", "two"},
{"http://one.two.three.four.five:9000", "two"},
{"http://s3.eu-central-2.wasabisys.com", "eu-central-2"},
{"http://s3.us-east-2.amazonaws.com", "us-east-2"},
{"https://s3.filebase.com", "us-east-1"},
}
assert := assert.New(t)
for _, pair := range cases {
Expand Down
11 changes: 4 additions & 7 deletions drivers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
// the future for optimized support of other storage providers.
uploaderPartSize = 63 * 1024 * 1024
// default region parameter if we can't derive one from the url
defaultIgnoredRegion = "ignored"
defaultIgnoredRegion = "us-east-1"
)

var _ OSSession = (*s3Session)(nil)
Expand Down Expand Up @@ -102,13 +102,10 @@ func customS3Region(host string) string {
}
hostname := u.Hostname()
parts := strings.Split(hostname, ".")
if len(parts) == 1 {
return hostname
if len(parts) >= 4 {
return parts[1]
}
if len(parts) == 2 {
return parts[0]
}
return parts[1]
return defaultIgnoredRegion
}

func newS3Session(info *S3OSInfo) OSSession {
Expand Down
22 changes: 22 additions & 0 deletions drivers/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ func TestStorjS3Read(t *testing.T) {
}
}

func TestFilebaseS3Read(t *testing.T) {
s3key := os.Getenv("FILEBASE_S3_KEY")
s3secret := os.Getenv("FILEBASE_S3_SECRET")
s3bucket := os.Getenv("FILEBASE_S3_BUCKET")
s3Path := os.Getenv("FILEBASE_S3_PATH")
require := require.New(t)
if s3key != "" && s3secret != "" && s3bucket != "" && s3Path != "" {
fullUrl := fmt.Sprintf("s3+https://%s:%[email protected]/%s", s3key, s3secret, s3bucket)
os, err := ParseOSURL(fullUrl, true)
require.NoError(err)
session := os.NewSession("")
data, err := session.ReadData(context.Background(), s3Path)
require.NoError(err)
osBuf := new(bytes.Buffer)
osBuf.ReadFrom(data.Body)
osData := osBuf.Bytes()
require.True(len(osData) > 0)
} else {
t.Skip("No S3 credentials, test skipped")
}
}

func TestWasabiS3Upload(t *testing.T) {
s3key := os.Getenv("WASABI_S3_KEY")
s3secret := os.Getenv("WASABI_S3_SECRET")
Expand Down

0 comments on commit b4d9ced

Please sign in to comment.