5
5
"bufio"
6
6
"crypto/md5"
7
7
"encoding/json"
8
+ "errors"
8
9
"fmt"
9
10
"io"
10
11
"io/ioutil"
@@ -22,6 +23,8 @@ import (
22
23
"github.com/aws/aws-sdk-go/aws"
23
24
"github.com/aws/aws-sdk-go/aws/awserr"
24
25
"github.com/aws/aws-sdk-go/aws/credentials"
26
+ "github.com/aws/aws-sdk-go/aws/defaults"
27
+
25
28
"github.com/aws/aws-sdk-go/aws/session"
26
29
"github.com/aws/aws-sdk-go/service/s3"
27
30
"github.com/aws/aws-sdk-go/service/s3/s3manager"
@@ -50,9 +53,21 @@ type MetaFile struct {
50
53
// Connect - connect to s3
51
54
func (s * S3 ) Connect () error {
52
55
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 )
53
68
if s .session , err = session .NewSession (
54
69
& aws.Config {
55
- Credentials : credentials . NewStaticCredentials ( s . Config . AccessKey , s . Config . SecretKey , "" ) ,
70
+ Credentials : creds ,
56
71
Region : aws .String (s .Config .Region ),
57
72
Endpoint : aws .String (s .Config .Endpoint ),
58
73
DisableSSL : aws .Bool (s .Config .DisableSSL ),
@@ -74,10 +89,11 @@ func (s *S3) Connect() error {
74
89
Scheme : httpSchema ,
75
90
PathStyle : s .Config .ForcePathStyle ,
76
91
}
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 )
81
97
s .s3Stream = s3StreamClient .Bucket (s .Config .Bucket )
82
98
83
99
return nil
@@ -761,3 +777,24 @@ func GetEtag(path string, partSize int64) string {
761
777
hash := md5 .Sum (contentToHash )
762
778
return fmt .Sprintf ("\" %x-%d\" " , hash , parts )
763
779
}
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