Skip to content

Commit

Permalink
Refactor aws config to include AWS envs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarta committed Dec 24, 2018
1 parent 0af18d1 commit 4833de3
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions pkg/skbn/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"io"
"os"
"path/filepath"
"strings"
"strconv"
"strings"

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

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/aws/endpoints"
)

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

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

awsConfig := &aws.Config{}

region := "eu-central-1"
if rg := os.Getenv("AWS_REGION"); rg != "" {
region = rg
}

endpoint := ""
if endp := os.Getenv("AWS_S3_ENDPOINT"); endp != "" {
endpoint = endp
} else {
resolver := endpoints.DefaultResolver()
resolvedEndpoint, _ := resolver.EndpointFor(endpoints.S3ServiceID, region)
/*if err != nil {
return fmt.Errorf("failed to resolve endpoint for given region: %s", region)
}*/
endpoint = resolvedEndpoint.URL
}

disableSSL := false
if disSSL := os.Getenv("AWS_S3_NO_SSL"); disSSL != "" {
disableSSL, _ = strconv.ParseBool(disSSL)
}

forcePathStyle := false
if fps := os.Getenv("AWS_S3_FORCE_PATH_STYLE"); fps != "" {
forcePathStyle, _ = strconv.ParseBool(fps)
}

s, err := session.NewSession(&aws.Config{
Region: aws.String(region),
Endpoint: aws.String(endpoint),
DisableSSL: aws.Bool(disableSSL),
S3ForcePathStyle: aws.Bool(forcePathStyle),
})

if rg := os.Getenv("AWS_REGION"); rg != "" {
region = rg
}

awsConfig.Region = aws.String(region)

if endpoint := os.Getenv("AWS_S3_ENDPOINT"); endpoint != "" {
awsConfig.Endpoint = aws.String(endpoint)
}

if disSSL := os.Getenv("AWS_S3_NO_SSL"); disSSL != "" {
disableSSL, _ := strconv.ParseBool(disSSL)
awsConfig.DisableSSL = aws.Bool(disableSSL)
}

if fps := os.Getenv("AWS_S3_FORCE_PATH_STYLE"); fps != "" {
forcePathStyle, _ := strconv.ParseBool(fps)
awsConfig.S3ForcePathStyle = aws.Bool(forcePathStyle)
}

s, err := session.NewSession(awsConfig)

return s, err
}
Expand Down

0 comments on commit 4833de3

Please sign in to comment.