I have a Spring Boot Application (SBApp) which uploads a file on S3. In the development phase, I'm using Localstack api to use the features of S3.
Problem
I want to connect to the Localstack S3 api runing in the docker container from SBApp.
I have two scenarios of running my SBApp :
I am using the following url to connect to the S3 bucket: http://s3.localhost.localstack.cloud:4566/
.
Using this url, I'm able to connect to the S3 when SBApp is running on the localhost. But when I'm running the same application in the docker container, the system reports this error :
Unable to execute HTTP request
It is not able to connect to the S3 from the docker container using the above mentioned url.
The following is my localstack image config:
localstack:
container_name: "SBApp-localstack"
image: localstack/localstack:3.0.0
hostname: localstack
networks:
- default
ports:
- "4566:4566"
healthcheck:
test: awslocal dynamodb list-tables
interval: 30s
timeout: 2s
retries: 5
environment:
- SERVICES=s3
- DEBUG=1
- DATA_DIR=/tmp/localstack/data
- AWS_REGION=ap-south-1
- AWS_ACCESS_KEY_ID=ABC
- AWS_SECRET_ACCESS_KEY=ABC
- NODE_ENV=test
command: bash -c "pip install awscli awscli-local requests --upgrade"
volumes:
- ./data:/var/lib/localstack
- "/var/run/docker.sock:/var/run/docker.sock"
- ./bin:/docker-entrypoint-initaws.d
I have tried the following other urls in both cases when the SBApp is running in the localhost and the docker container-
http://localhost:4566/
(in this case the system prefixes the bucket name like - bucket_name.localhost)http://localstack:4566/
(in this case the system prefixes the bucket name like - bucket_name.localstack)http://SBApp-bucket.s3.localhost.localstack.cloud:4566/
But none is working fine. It would highly appreciable if someone could correct me in case I'm doing something wrong Or give me the correct solution.
As mentioned previously, you need to use the Path Style configuration, or use s3 prefix in the endpoint name. https://docs.localstack.cloud/user-guide/aws/s3/#path-style-and-virtual-hosted-style-requests