...

Source file src/github.com/aws/aws-sdk-go-v2/config/example_test.go

Documentation: github.com/aws/aws-sdk-go-v2/config

     1  package config_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"path/filepath"
     9  	"time"
    10  
    11  	"github.com/aws/aws-sdk-go-v2/aws"
    12  	awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
    13  	"github.com/aws/aws-sdk-go-v2/config"
    14  	"github.com/aws/aws-sdk-go-v2/credentials"
    15  	"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
    16  	"github.com/aws/smithy-go/middleware"
    17  	smithyhttp "github.com/aws/smithy-go/transport/http"
    18  )
    19  
    20  func ExampleWithCredentialsCacheOptions() {
    21  	cfg, err := config.LoadDefaultConfig(context.TODO(),
    22  		config.WithCredentialsCacheOptions(func(o *aws.CredentialsCacheOptions) {
    23  			o.ExpiryWindow = 10 * time.Minute
    24  		}),
    25  	)
    26  	if err != nil {
    27  		log.Fatal(err)
    28  	}
    29  	_ = cfg
    30  }
    31  
    32  func ExampleWithSharedConfigProfile() {
    33  	cfg, err := config.LoadDefaultConfig(context.TODO(),
    34  		// Specify the shared configuration profile to load.
    35  		config.WithSharedConfigProfile("exampleProfile"),
    36  
    37  		// Optionally specify the specific shared configuraiton
    38  		// files to load the profile from.
    39  		config.WithSharedConfigFiles([]string{
    40  			filepath.Join("testdata", "shared_config"),
    41  		}),
    42  	)
    43  	if err != nil {
    44  		log.Fatal(err)
    45  	}
    46  
    47  	// Region loaded from credentials file.
    48  	fmt.Println("Region:", cfg.Region)
    49  
    50  	// Output:
    51  	// Region: us-west-2
    52  }
    53  
    54  func ExampleWithCredentialsProvider() {
    55  	cfg, err := config.LoadDefaultConfig(context.TODO(),
    56  		// Hard coded credentials.
    57  		config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
    58  			Value: aws.Credentials{
    59  				AccessKeyID: "AKID", SecretAccessKey: "SECRET", SessionToken: "SESSION",
    60  				Source: "example hard coded credentials",
    61  			},
    62  		}))
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  
    67  	// Credentials retrieve will be called automatically internally to the SDK
    68  	// service clients created with the cfg value.
    69  	creds, err := cfg.Credentials.Retrieve(context.TODO())
    70  	if err != nil {
    71  		log.Fatal(err)
    72  	}
    73  
    74  	fmt.Println("Credentials Source:", creds.Source)
    75  	// Credentials Source: example hard coded credentials
    76  }
    77  
    78  func ExampleWithAPIOptions() {
    79  	// import "github.com/aws/smithy-go/middleware"
    80  	// import smithyhttp "github.com/aws/smithy-go/transport/http"
    81  
    82  	cfg, err := config.LoadDefaultConfig(context.TODO(),
    83  		config.WithAPIOptions([]func(*middleware.Stack) error{
    84  			smithyhttp.AddHeaderValue("X-Custom-Header", "customHeaderValue"),
    85  		}),
    86  	)
    87  	if err != nil {
    88  		log.Fatal(err)
    89  	}
    90  	_ = cfg
    91  }
    92  
    93  func ExampleWithEndpointResolver() {
    94  	cfg, err := config.LoadDefaultConfig(context.TODO(),
    95  		config.WithEndpointResolver(aws.EndpointResolverFunc(
    96  			func(service, region string) (aws.Endpoint, error) {
    97  				return aws.Endpoint{URL: "https://mock.amazonaws.com"}, nil
    98  			})),
    99  	)
   100  
   101  	if err != nil {
   102  		log.Fatal(err)
   103  	}
   104  	_ = cfg
   105  }
   106  
   107  func ExampleWithEndpointResolverWithOptions() {
   108  	cfg, err := config.LoadDefaultConfig(context.TODO(),
   109  		config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(
   110  			func(service, region string, options ...interface{}) (aws.Endpoint, error) {
   111  				return aws.Endpoint{URL: "https://mock.amazonaws.com"}, nil
   112  			})),
   113  	)
   114  
   115  	if err != nil {
   116  		log.Fatal(err)
   117  	}
   118  	_ = cfg
   119  }
   120  
   121  func ExampleWithHTTPClient() {
   122  	cfg, err := config.LoadDefaultConfig(context.TODO(),
   123  		config.WithHTTPClient(awshttp.NewBuildableClient().
   124  			WithTransportOptions(func(tr *http.Transport) {
   125  				tr.MaxIdleConns = 60
   126  			})),
   127  	)
   128  	if err != nil {
   129  		log.Fatal(err)
   130  	}
   131  	_ = cfg
   132  }
   133  
   134  func ExampleWithWebIdentityRoleCredentialOptions() {
   135  	cfg, err := config.LoadDefaultConfig(context.TODO(),
   136  		config.WithWebIdentityRoleCredentialOptions(func(options *stscreds.WebIdentityRoleOptions) {
   137  			options.RoleSessionName = "customSessionName"
   138  		}))
   139  	if err != nil {
   140  		log.Fatal(err)
   141  	}
   142  	_ = cfg
   143  }
   144  
   145  func ExampleWithRegion() {
   146  	cfg, err := config.LoadDefaultConfig(context.TODO(),
   147  		config.WithRegion("us-west-2"))
   148  	if err != nil {
   149  		log.Fatal(err)
   150  	}
   151  	_ = cfg
   152  }
   153  
   154  func ExampleWithEC2IMDSRegion() {
   155  	cfg, err := config.LoadDefaultConfig(context.TODO(),
   156  		config.WithEC2IMDSRegion(),
   157  	)
   158  	if err != nil {
   159  		log.Fatal(err)
   160  	}
   161  	_ = cfg
   162  }
   163  
   164  func ExampleWithAssumeRoleCredentialOptions() {
   165  	// WithAssumeRoleCredentialOptions can be used to configure the AssumeRoleOptions for the STS credential provider.
   166  	// For example the TokenProvider can be populated if assuming a role that requires an MFA token.
   167  	cfg, err := config.LoadDefaultConfig(context.TODO(),
   168  		config.WithAssumeRoleCredentialOptions(func(options *stscreds.AssumeRoleOptions) {
   169  			options.TokenProvider = func() (string, error) {
   170  				return "theTokenCode", nil
   171  			}
   172  		}))
   173  	if err != nil {
   174  		log.Fatal(err)
   175  	}
   176  	_ = cfg
   177  }
   178  

View as plain text