forked from cloudfoundry-attic/bosh-ext-cli
/
sdk.go
51 lines (40 loc) · 1.29 KB
/
sdk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package client
import (
boshhttp "github.com/cloudfoundry/bosh-utils/httpclient"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/cloudfoundry/bosh-s3cli/config"
)
func NewSDK(c config.S3Cli) (*s3.S3, error) {
var httpClient *http.Client
if c.SSLVerifyPeer {
httpClient = boshhttp.CreateDefaultClient(nil)
} else {
httpClient = boshhttp.CreateDefaultClientInsecureSkipVerify()
}
s3Config := aws.NewConfig().
WithLogLevel(aws.LogOff).
WithS3ForcePathStyle(true).
WithDisableSSL(!c.UseSSL).
WithHTTPClient(httpClient)
if c.UseRegion() {
s3Config = s3Config.WithRegion(c.Region).WithEndpoint(c.S3Endpoint())
} else {
s3Config = s3Config.WithRegion(config.EmptyRegion).WithEndpoint(c.S3Endpoint())
}
if c.CredentialsSource == config.StaticCredentialsSource {
s3Config = s3Config.WithCredentials(credentials.NewStaticCredentials(c.AccessKeyID, c.SecretAccessKey, ""))
}
if c.CredentialsSource == config.NoneCredentialsSource {
s3Config = s3Config.WithCredentials(credentials.AnonymousCredentials)
}
s3Session := session.New(s3Config)
s3Client := s3.New(s3Session)
if c.UseV2SigningMethod {
setv2Handlers(s3Client)
}
return s3Client, nil
}