Merge pull request #5 from jacksgt/http-protocol

Allow user to specify protocol for custom endpoint
This commit is contained in:
Rohan Verma
2020-09-15 13:26:09 +05:30
committed by GitHub
2 changed files with 27 additions and 1 deletions
+5 -1
View File
@@ -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)
}
@@ -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
+22
View File
@@ -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)
}
}