Skip to content

Commit 3199580

Browse files
alexandrst88AlexAkulov
authored andcommitted
Fixed process of the retrieving aws credentials (#29)
1 parent ba996f1 commit 3199580

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

s3.go

+42-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bufio"
66
"crypto/md5"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"io"
1011
"io/ioutil"
@@ -22,6 +23,8 @@ import (
2223
"github.com/aws/aws-sdk-go/aws"
2324
"github.com/aws/aws-sdk-go/aws/awserr"
2425
"github.com/aws/aws-sdk-go/aws/credentials"
26+
"github.com/aws/aws-sdk-go/aws/defaults"
27+
2528
"github.com/aws/aws-sdk-go/aws/session"
2629
"github.com/aws/aws-sdk-go/service/s3"
2730
"github.com/aws/aws-sdk-go/service/s3/s3manager"
@@ -50,9 +53,21 @@ type MetaFile struct {
5053
// Connect - connect to s3
5154
func (s *S3) Connect() error {
5255
var err error
56+
awsDefaults := defaults.Get()
57+
defaultCredProviders := defaults.CredProviders(awsDefaults.Config, awsDefaults.Handlers)
58+
59+
// Define custom static cred provider
60+
staticCreds := &credentials.StaticProvider{Value: credentials.Value{
61+
AccessKeyID: s.Config.AccessKey,
62+
SecretAccessKey: s.Config.SecretKey,
63+
}}
64+
65+
// Append static creds to the defaults
66+
customCredProviders := append([]credentials.Provider{staticCreds}, defaultCredProviders...)
67+
creds := credentials.NewChainCredentials(customCredProviders)
5368
if s.session, err = session.NewSession(
5469
&aws.Config{
55-
Credentials: credentials.NewStaticCredentials(s.Config.AccessKey, s.Config.SecretKey, ""),
70+
Credentials: creds,
5671
Region: aws.String(s.Config.Region),
5772
Endpoint: aws.String(s.Config.Endpoint),
5873
DisableSSL: aws.Bool(s.Config.DisableSSL),
@@ -74,10 +89,11 @@ func (s *S3) Connect() error {
7489
Scheme: httpSchema,
7590
PathStyle: s.Config.ForcePathStyle,
7691
}
77-
s3StreamClient := s3gof3r.New(endpoint, s.Config.Region, s3gof3r.Keys{
78-
AccessKey: s.Config.AccessKey,
79-
SecretKey: s.Config.SecretKey,
80-
})
92+
keys, err := s.getAWSKeys()
93+
if err != nil {
94+
return err
95+
}
96+
s3StreamClient := s3gof3r.New(endpoint, s.Config.Region, keys)
8197
s.s3Stream = s3StreamClient.Bucket(s.Config.Bucket)
8298

8399
return nil
@@ -761,3 +777,24 @@ func GetEtag(path string, partSize int64) string {
761777
hash := md5.Sum(contentToHash)
762778
return fmt.Sprintf("\"%x-%d\"", hash, parts)
763779
}
780+
781+
func (s *S3) getAWSKeys() (keys s3gof3r.Keys, err error) {
782+
783+
if s.Config.AccessKey != "" && s.Config.SecretKey != "" {
784+
return s3gof3r.Keys{
785+
AccessKey: s.Config.AccessKey,
786+
SecretKey: s.Config.SecretKey,
787+
}, nil
788+
}
789+
790+
keys, err = s3gof3r.EnvKeys()
791+
if err == nil {
792+
return
793+
}
794+
keys, err = s3gof3r.InstanceKeys()
795+
if err == nil {
796+
return
797+
}
798+
err = errors.New("no AWS keys found")
799+
return
800+
}

0 commit comments

Comments
 (0)