localstack + docker-compose + S3
I got a service in Java with Spring-boot + spring-cloud-aws-messaging ... that uploads files into S3 ...
It's failing when tries to upload a file into S3 bucket (only when I run it in docker-compose).
here is my code
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-messaging</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
S3 client
public AmazonS3 amazonS3Client() {
return AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(appConfig.getAwsS3Endpoint(), appConfig.getRegion()))
.withCredentials(credentialsProvider)
.build();
}
docker-compose yaml
version: '3.7'
services:
dumb-service:
build: ../dumb-service/.
image: dumb-service:latest
hostname: dumb-service
ports:
- '8080:8080'
- '5006:5006'
environment:
JAVA_OPTS: '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5006'
AWS_S3_ENDPOINT: 'http://localstack:4566/'
AWS_S3_BUCKET: 'dumb-bucket'
AWS_SQS_ENDPOINT: 'http://localstack:4566'
PDF_REQUEST_QUEUE_URL: 'http://localstack:4566/000000000000/dumb-inbound.fifo'
PDF_REQUEST_QUEUE_NAME: 'dumb-inbound.fifo'
AWS_REGION: "${AWS_REGION}"
AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
depends_on:
- "localstack"
# localstack home: https://github.com/localstack/localstack
localstack:
hostname: localstack
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
image: localstack/localstack
ports:
- '4566:4566'
- '4571:4571'
environment:
- SERVICES=${SERVICES-}
- DEBUG=${DEBUG-}
- DATA_DIR=${DATA_DIR-}
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
- HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
- DOCKER_HOST=unix:///var/run/docker.sock
volumes:
- "${TMPDIR:-/tmp}/localstack:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
Error is:
Unable to execute HTTP request: dumb-bucket.localstack
(This code is working fine with AWS services or running code native + localstack)
Any thoughts about how to connect to localstack S3 ?
Solution 1:
If you are trying to access an s3 bucket with localstack via an aws api; you would need to withPathStyleEnabled
flag turned on.
for eg:
AmazonS3 createLocalStackS3WithEndpointConfig(String endpointUrl, String awsRegion) {
return createAwsBeanWithEndpointConfig(
endpointUrl, awsRegion,
epConf -> AmazonS3ClientBuilder.standard().withEndpointConfiguration(epConf).withPathStyleAccessEnabled(true).build(),
AmazonS3ClientBuilder::defaultClient
);
} `
Reference : Troubleshooting section of https://pythonrepo.com/repo/localstack-localstack