Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 3af85c6

Browse files
authored
docs: update localstack example for testcontainers-go
1 parent 362d7e5 commit 3af85c6

File tree

1 file changed

+44
-40
lines changed
  • content/en/user-guide/integrations/testcontainers

1 file changed

+44
-40
lines changed

content/en/user-guide/integrations/testcontainers/index.md

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ await localStackContainer.StartAsync()
5858
.ConfigureAwait(false);
5959
{{< /tab >}}
6060
{{< tab header="Go" lang="go">}}
61-
container, err := localstack.StartContainer(ctx, localstack.NoopOverrideContainerRequest)
61+
container, err := localstack.Run(ctx, "localstack/localstack:latest")
6262
{{< /tab >}}
6363
{{< tab header="Java" lang="java">}}
6464
LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:3"));
@@ -77,46 +77,50 @@ config.ServiceURL = localStackContainer.GetConnectionString();
7777
using var client = new AmazonS3Client(config);
7878
{{< /tab >}}
7979
{{< tab header="Go" lang="go">}}
80+
type resolverV2 struct {
81+
// you could inject additional application context here as well
82+
}
83+
84+
func (*resolverV2) ResolveEndpoint(ctx context.Context, params s3.EndpointParameters) (
85+
smithyendpoints.Endpoint, error,
86+
) {
87+
// delegate back to the default v2 resolver otherwise
88+
return s3.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params)
89+
}
90+
8091
func s3Client(ctx context.Context, l *localstack.LocalStackContainer) (*s3.Client, error) {
81-
// the Testcontainers Docker provider is used to get the host of the Docker daemon
82-
provider, err := testcontainers.NewDockerProvider()
83-
if err != nil {
84-
return nil, err
85-
}
86-
87-
host, err := provider.DaemonHost(ctx)
88-
if err != nil {
89-
return nil, err
90-
}
91-
92-
mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp"))
93-
if err != nil {
94-
return nil, err
95-
}
96-
97-
customResolver := aws.EndpointResolverWithOptionsFunc(
98-
func(service, region string, opts ...interface{}) (aws.Endpoint, error) {
99-
return aws.Endpoint{
100-
PartitionID: "aws",
101-
URL: fmt.Sprintf("http://%s:%d", host, mappedPort.Int()),
102-
SigningRegion: region,
103-
}, nil
104-
})
105-
106-
awsCfg, err := config.LoadDefaultConfig(context.TODO(),
107-
config.WithRegion(region),
108-
config.WithEndpointResolverWithOptions(customResolver),
109-
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accesskey, secretkey, token)),
110-
)
111-
if err != nil {
112-
return nil, err
113-
}
114-
115-
client := s3.NewFromConfig(awsCfg, func(o *s3.Options) {
116-
o.UsePathStyle = true
117-
})
118-
119-
return client, nil
92+
mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp"))
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
provider, err := testcontainers.NewDockerProvider()
98+
if err != nil {
99+
return nil, err
100+
}
101+
defer provider.Close()
102+
103+
host, err := provider.DaemonHost(ctx)
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
awsCfg, err := config.LoadDefaultConfig(context.TODO(),
109+
config.WithRegion(region),
110+
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accesskey, secretkey, token)),
111+
)
112+
if err != nil {
113+
return nil, err
114+
}
115+
116+
// reference: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/endpoints/#with-both
117+
client := s3.NewFromConfig(awsCfg, func(o *s3.Options) {
118+
o.BaseEndpoint = aws.String("http://" + host + ":" + mappedPort.Port())
119+
o.EndpointResolverV2 = &resolverV2{}
120+
o.UsePathStyle = true
121+
})
122+
123+
return client, nil
120124
}
121125
{{< /tab >}}
122126
{{< tab header="Java" lang="java">}}

0 commit comments

Comments
 (0)