/
example_test.go
164 lines (144 loc) · 4.14 KB
/
example_test.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package config_test
import (
"context"
"fmt"
"log"
"net/http"
"path/filepath"
"github.com/aws/aws-sdk-go-v2/aws"
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)
func ExampleWithSharedConfigProfile() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
// Specify the shared configuration profile to load.
config.WithSharedConfigProfile("exampleProfile"),
// Optionally specify the specific shared configuraiton
// files to load the profile from.
config.WithSharedConfigFiles([]string{
filepath.Join("testdata", "shared_config"),
}),
)
if err != nil {
log.Fatal(err)
}
// Region loaded from credentials file.
fmt.Println("Region:", cfg.Region)
// Output:
// Region: us-west-2
}
func ExampleWithCredentialsProvider() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
// Hard coded credentials.
config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: "AKID", SecretAccessKey: "SECRET", SessionToken: "SESSION",
Source: "example hard coded credentials",
},
}))
if err != nil {
log.Fatal(err)
}
// Credentials retrieve will be called automatically internally to the SDK
// service clients created with the cfg value.
creds, err := cfg.Credentials.Retrieve(context.TODO())
if err != nil {
log.Fatal(err)
}
fmt.Println("Credentials Source:", creds.Source)
// Credentials Source: example hard coded credentials
}
func ExampleWithAPIOptions() {
// import "github.com/aws/smithy-go/middleware"
// import smithyhttp "github.com/aws/smithy-go/transport/http"
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithAPIOptions([]func(*middleware.Stack) error{
smithyhttp.AddHeaderValue("X-Custom-Header", "customHeaderValue"),
}),
)
if err != nil {
log.Fatal(err)
}
_ = cfg
}
func ExampleWithEndpointResolver() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithEndpointResolver(aws.EndpointResolverFunc(
func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{URL: "https://mock.amazonaws.com"}, nil
})),
)
if err != nil {
log.Fatal(err)
}
_ = cfg
}
func ExampleWithEndpointResolverWithOptions() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{URL: "https://mock.amazonaws.com"}, nil
})),
)
if err != nil {
log.Fatal(err)
}
_ = cfg
}
func ExampleWithHTTPClient() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithHTTPClient(awshttp.NewBuildableClient().
WithTransportOptions(func(tr *http.Transport) {
tr.MaxIdleConns = 60
})),
)
if err != nil {
log.Fatal(err)
}
_ = cfg
}
func ExampleWithWebIdentityRoleCredentialOptions() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithWebIdentityRoleCredentialOptions(func(options *stscreds.WebIdentityRoleOptions) {
options.RoleSessionName = "customSessionName"
}))
if err != nil {
log.Fatal(err)
}
_ = cfg
}
func ExampleWithRegion() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-west-2"))
if err != nil {
log.Fatal(err)
}
_ = cfg
}
func ExampleWithEC2IMDSRegion() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithEC2IMDSRegion(),
)
if err != nil {
log.Fatal(err)
}
_ = cfg
}
func ExampleWithAssumeRoleCredentialOptions() {
// WithAssumeRoleCredentialOptions can be used to configure the AssumeRoleOptions for the STS credential provider.
// For example the TokenProvider can be populated if assuming a role that requires an MFA token.
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithAssumeRoleCredentialOptions(func(options *stscreds.AssumeRoleOptions) {
options.TokenProvider = func() (string, error) {
return "theTokenCode", nil
}
}))
if err != nil {
log.Fatal(err)
}
_ = cfg
}