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
|
//go:build example
// +build example
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sqs"
)
func main() {
defaultResolver := endpoints.DefaultResolver()
s3CustResolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
if service == "s3" {
return endpoints.ResolvedEndpoint{
URL: "https://s3.custom.endpoint.com",
SigningRegion: "custom-signing-region",
}, nil
}
return defaultResolver.EndpointFor(service, region, optFns...)
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("us-west-2"),
EndpointResolver: endpoints.ResolverFunc(s3CustResolverFn),
},
}))
// Create the S3 service client with the shared session. This will
// automatically use the S3 custom endpoint configured in the custom
// endpoint resolver wrapping the default endpoint resolver.
s3Svc := s3.New(sess)
// Operation calls will be made to the custom endpoint.
s3Svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String("myBucket"),
Key: aws.String("myObjectKey"),
})
// Create the SQS service client with the shared session. This will
// fallback to the default endpoint resolver because the customization
// passes any non S3 service endpoint resolve to the default resolver.
sqsSvc := sqs.New(sess)
// Operation calls will be made to the default endpoint for SQS for the
// region configured.
sqsSvc.ReceiveMessage(&sqs.ReceiveMessageInput{
QueueUrl: aws.String("my-queue-url"),
})
// Create a DynamoDB service client that will use a custom endpoint
// resolver that overrides the shared session's. This is useful when
// custom endpoints are generated, or multiple endpoints are switched on
// by a region value.
ddbCustResolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
return endpoints.ResolvedEndpoint{
URL: "dynamodb.custom.endpoint",
SigningRegion: "custom-signing-region",
}, nil
}
ddbSvc := dynamodb.New(sess, &aws.Config{
EndpointResolver: endpoints.ResolverFunc(ddbCustResolverFn),
})
// Operation calls will be made to the custom endpoint set in the
// ddCustResolverFn.
ddbSvc.ListTables(&dynamodb.ListTablesInput{})
// Setting Config's Endpoint will override the EndpointResolver. Forcing
// the service client to make all operation to the endpoint specified
// the in the config.
ddbSvcLocal := dynamodb.New(sess, &aws.Config{
Endpoint: aws.String("http://localhost:8088"),
})
ddbSvcLocal.ListTables(&dynamodb.ListTablesInput{})
}
|