Skip to content

Commit 12d508b

Browse files
committed
Merge branch 'master' of github.com:AlexAkulov/clickhouse-backup
2 parents 0e4f128 + 3199580 commit 12d508b

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

Diff for: 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
@@ -767,3 +783,24 @@ func GetEtag(path string, partSize int64) string {
767783
hash := md5.Sum(contentToHash)
768784
return fmt.Sprintf("\"%x-%d\"", hash, parts)
769785
}
786+
787+
func (s *S3) getAWSKeys() (keys s3gof3r.Keys, err error) {
788+
789+
if s.Config.AccessKey != "" && s.Config.SecretKey != "" {
790+
return s3gof3r.Keys{
791+
AccessKey: s.Config.AccessKey,
792+
SecretKey: s.Config.SecretKey,
793+
}, nil
794+
}
795+
796+
keys, err = s3gof3r.EnvKeys()
797+
if err == nil {
798+
return
799+
}
800+
keys, err = s3gof3r.InstanceKeys()
801+
if err == nil {
802+
return
803+
}
804+
err = errors.New("no AWS keys found")
805+
return
806+
}

0 commit comments

Comments
 (0)