Skip to content

Commit

Permalink
Fixed process of the retrieving aws credentials (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrst88 authored and AlexAkulov committed May 24, 2019
1 parent ba996f1 commit 3199580
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bufio"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -22,6 +23,8 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"

"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"
Expand Down Expand Up @@ -50,9 +53,21 @@ type MetaFile struct {
// Connect - connect to s3
func (s *S3) Connect() error {
var err error
awsDefaults := defaults.Get()
defaultCredProviders := defaults.CredProviders(awsDefaults.Config, awsDefaults.Handlers)

// Define custom static cred provider
staticCreds := &credentials.StaticProvider{Value: credentials.Value{
AccessKeyID: s.Config.AccessKey,
SecretAccessKey: s.Config.SecretKey,
}}

// Append static creds to the defaults
customCredProviders := append([]credentials.Provider{staticCreds}, defaultCredProviders...)
creds := credentials.NewChainCredentials(customCredProviders)
if s.session, err = session.NewSession(
&aws.Config{
Credentials: credentials.NewStaticCredentials(s.Config.AccessKey, s.Config.SecretKey, ""),
Credentials: creds,
Region: aws.String(s.Config.Region),
Endpoint: aws.String(s.Config.Endpoint),
DisableSSL: aws.Bool(s.Config.DisableSSL),
Expand All @@ -74,10 +89,11 @@ func (s *S3) Connect() error {
Scheme: httpSchema,
PathStyle: s.Config.ForcePathStyle,
}
s3StreamClient := s3gof3r.New(endpoint, s.Config.Region, s3gof3r.Keys{
AccessKey: s.Config.AccessKey,
SecretKey: s.Config.SecretKey,
})
keys, err := s.getAWSKeys()
if err != nil {
return err
}
s3StreamClient := s3gof3r.New(endpoint, s.Config.Region, keys)
s.s3Stream = s3StreamClient.Bucket(s.Config.Bucket)

return nil
Expand Down Expand Up @@ -761,3 +777,24 @@ func GetEtag(path string, partSize int64) string {
hash := md5.Sum(contentToHash)
return fmt.Sprintf("\"%x-%d\"", hash, parts)
}

func (s *S3) getAWSKeys() (keys s3gof3r.Keys, err error) {

if s.Config.AccessKey != "" && s.Config.SecretKey != "" {
return s3gof3r.Keys{
AccessKey: s.Config.AccessKey,
SecretKey: s.Config.SecretKey,
}, nil
}

keys, err = s3gof3r.EnvKeys()
if err == nil {
return
}
keys, err = s3gof3r.InstanceKeys()
if err == nil {
return
}
err = errors.New("no AWS keys found")
return
}

0 comments on commit 3199580

Please sign in to comment.