Skip to content

Commit 4833de3

Browse files
committed
Refactor aws config to include AWS envs
1 parent 0af18d1 commit 4833de3

1 file changed

Lines changed: 25 additions & 34 deletions

File tree

pkg/skbn/s3.go

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import (
55
"io"
66
"os"
77
"path/filepath"
8-
"strings"
98
"strconv"
9+
"strings"
1010

1111
"github.com/nuvo/skbn/pkg/utils"
1212

1313
"github.com/aws/aws-sdk-go/aws"
1414
"github.com/aws/aws-sdk-go/aws/session"
1515
"github.com/aws/aws-sdk-go/service/s3"
1616
"github.com/aws/aws-sdk-go/service/s3/s3manager"
17-
"github.com/aws/aws-sdk-go/aws/endpoints"
1817
)
1918

2019
// GetClientToS3 checks the connection to S3 and returns the tested client
@@ -174,39 +173,31 @@ func UploadToS3(iClient interface{}, toPath, fromPath string, reader io.Reader)
174173

175174
func getNewSession() (*session.Session, error) {
176175

176+
awsConfig := &aws.Config{}
177+
177178
region := "eu-central-1"
178-
if rg := os.Getenv("AWS_REGION"); rg != "" {
179-
region = rg
180-
}
181-
182-
endpoint := ""
183-
if endp := os.Getenv("AWS_S3_ENDPOINT"); endp != "" {
184-
endpoint = endp
185-
} else {
186-
resolver := endpoints.DefaultResolver()
187-
resolvedEndpoint, _ := resolver.EndpointFor(endpoints.S3ServiceID, region)
188-
/*if err != nil {
189-
return fmt.Errorf("failed to resolve endpoint for given region: %s", region)
190-
}*/
191-
endpoint = resolvedEndpoint.URL
192-
}
193-
194-
disableSSL := false
195-
if disSSL := os.Getenv("AWS_S3_NO_SSL"); disSSL != "" {
196-
disableSSL, _ = strconv.ParseBool(disSSL)
197-
}
198-
199-
forcePathStyle := false
200-
if fps := os.Getenv("AWS_S3_FORCE_PATH_STYLE"); fps != "" {
201-
forcePathStyle, _ = strconv.ParseBool(fps)
202-
}
203-
204-
s, err := session.NewSession(&aws.Config{
205-
Region: aws.String(region),
206-
Endpoint: aws.String(endpoint),
207-
DisableSSL: aws.Bool(disableSSL),
208-
S3ForcePathStyle: aws.Bool(forcePathStyle),
209-
})
179+
180+
if rg := os.Getenv("AWS_REGION"); rg != "" {
181+
region = rg
182+
}
183+
184+
awsConfig.Region = aws.String(region)
185+
186+
if endpoint := os.Getenv("AWS_S3_ENDPOINT"); endpoint != "" {
187+
awsConfig.Endpoint = aws.String(endpoint)
188+
}
189+
190+
if disSSL := os.Getenv("AWS_S3_NO_SSL"); disSSL != "" {
191+
disableSSL, _ := strconv.ParseBool(disSSL)
192+
awsConfig.DisableSSL = aws.Bool(disableSSL)
193+
}
194+
195+
if fps := os.Getenv("AWS_S3_FORCE_PATH_STYLE"); fps != "" {
196+
forcePathStyle, _ := strconv.ParseBool(fps)
197+
awsConfig.S3ForcePathStyle = aws.Bool(forcePathStyle)
198+
}
199+
200+
s, err := session.NewSession(awsConfig)
210201

211202
return s, err
212203
}

0 commit comments

Comments
 (0)