github.com/aavshr/aws-sdk-go@v1.41.3/example/aws/endpoints/customEndpoint/customEndpoint.go (about) 1 //go:build example 2 // +build example 3 4 package main 5 6 import ( 7 "github.com/aavshr/aws-sdk-go/aws" 8 "github.com/aavshr/aws-sdk-go/aws/endpoints" 9 "github.com/aavshr/aws-sdk-go/aws/session" 10 "github.com/aavshr/aws-sdk-go/service/dynamodb" 11 "github.com/aavshr/aws-sdk-go/service/s3" 12 "github.com/aavshr/aws-sdk-go/service/sqs" 13 ) 14 15 func main() { 16 defaultResolver := endpoints.DefaultResolver() 17 s3CustResolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) { 18 if service == "s3" { 19 return endpoints.ResolvedEndpoint{ 20 URL: "https://s3.custom.endpoint.com", 21 SigningRegion: "custom-signing-region", 22 }, nil 23 } 24 25 return defaultResolver.EndpointFor(service, region, optFns...) 26 } 27 sess := session.Must(session.NewSessionWithOptions(session.Options{ 28 Config: aws.Config{ 29 Region: aws.String("us-west-2"), 30 EndpointResolver: endpoints.ResolverFunc(s3CustResolverFn), 31 }, 32 })) 33 34 // Create the S3 service client with the shared session. This will 35 // automatically use the S3 custom endpoint configured in the custom 36 // endpoint resolver wrapping the default endpoint resolver. 37 s3Svc := s3.New(sess) 38 // Operation calls will be made to the custom endpoint. 39 s3Svc.GetObject(&s3.GetObjectInput{ 40 Bucket: aws.String("myBucket"), 41 Key: aws.String("myObjectKey"), 42 }) 43 44 // Create the SQS service client with the shared session. This will 45 // fallback to the default endpoint resolver because the customization 46 // passes any non S3 service endpoint resolve to the default resolver. 47 sqsSvc := sqs.New(sess) 48 // Operation calls will be made to the default endpoint for SQS for the 49 // region configured. 50 sqsSvc.ReceiveMessage(&sqs.ReceiveMessageInput{ 51 QueueUrl: aws.String("my-queue-url"), 52 }) 53 54 // Create a DynamoDB service client that will use a custom endpoint 55 // resolver that overrides the shared session's. This is useful when 56 // custom endpoints are generated, or multiple endpoints are switched on 57 // by a region value. 58 ddbCustResolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) { 59 return endpoints.ResolvedEndpoint{ 60 URL: "dynamodb.custom.endpoint", 61 SigningRegion: "custom-signing-region", 62 }, nil 63 } 64 ddbSvc := dynamodb.New(sess, &aws.Config{ 65 EndpointResolver: endpoints.ResolverFunc(ddbCustResolverFn), 66 }) 67 // Operation calls will be made to the custom endpoint set in the 68 // ddCustResolverFn. 69 ddbSvc.ListTables(&dynamodb.ListTablesInput{}) 70 71 // Setting Config's Endpoint will override the EndpointResolver. Forcing 72 // the service client to make all operation to the endpoint specified 73 // the in the config. 74 ddbSvcLocal := dynamodb.New(sess, &aws.Config{ 75 Endpoint: aws.String("http://localhost:8088"), 76 }) 77 ddbSvcLocal.ListTables(&dynamodb.ListTablesInput{}) 78 }