Skip to content

Commit

Permalink
Merge pull request #5 from jacksgt/http-protocol
Browse files Browse the repository at this point in the history
Allow user to specify protocol for custom endpoint
  • Loading branch information
rhnvrm committed Sep 15, 2020
2 parents 2a542b0 + 2c7094b commit 59b9f0b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion simples3.go
Expand Up @@ -155,7 +155,7 @@ func (s3 *S3) getClient() *http.Client {

func (s3 *S3) getURL(bucket string, args ...string) (uri string) {
if len(s3.Endpoint) > 0 {
uri = defaultProtocol + s3.Endpoint + "/" + bucket
uri = s3.Endpoint + "/" + bucket
} else {
uri = fmt.Sprintf(s3.URIFormat, s3.Region, bucket)
}
Expand All @@ -168,8 +168,12 @@ func (s3 *S3) getURL(bucket string, args ...string) (uri string) {

// SetEndpoint can be used to the set a custom endpoint for
// using an alternate instance compatible with the s3 API.
// If no protocol is included in the URI, defaults to HTTPS.
func (s3 *S3) SetEndpoint(uri string) *S3 {
if len(uri) > 0 {
if !strings.HasPrefix(uri, "http") {
uri = "https://" + uri
}
s3.Endpoint = uri
}
return s3
Expand Down
22 changes: 22 additions & 0 deletions simples3_test.go
Expand Up @@ -272,3 +272,25 @@ func TestS3_NewUsingIAM(t *testing.T) {
t.Errorf("S3.FileDelete() got = %v", s3)
}
}

func TestCustomEndpoint(t *testing.T) {
s3 := New("us-east-1", "AccessKey", "SuperSecretKey")

// no protocol specified, should default to https
s3.SetEndpoint("example.com")
if s3.getURL("bucket1") != "https://example.com/bucket1" {
t.Errorf("S3.SetEndpoint() got = %v", s3.Endpoint)
}

// explicit http protocol
s3.SetEndpoint("http://localhost:9000")
if s3.getURL("bucket2") != "http://localhost:9000/bucket2" {
t.Errorf("S3.SetEndpoint() got = %v", s3.Endpoint)
}

// explicit http protocol
s3.SetEndpoint("https://example.com")
if s3.getURL("bucket3") != "https://example.com/bucket3" {
t.Errorf("S3.SetEndpoint() got = %v", s3.Endpoint)
}
}

0 comments on commit 59b9f0b

Please sign in to comment.