在我的 java 应用程序中,我正在创建以下 S3 客户端:
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
我的 docker 文件:
FROM openjdk:11.0.7-jre-slim
EXPOSE 8080
COPY target/myjar.jar /usr/app/
WORKDIR /usr/app
ENTRYPOINT ["java", "-jar", "myjar.jar"]
我将 java 应用程序部署到本地容器,并使用了以下运行命令:
$ docker run my-image-name --net="host" -e AWS_ACCESS_KEY_ID=XXXXXX -e AWS_SECRET_ACCESS_KEY=XXXXXX -e AWS_DEFAULT_REGION=eu-central-1
但是当我的 springboot 应用程序启动时,我在 s3Client 初始化时遇到异常:
com.amazonaws.SdkClientException: Failed to connect to service endpoint:
......
Caused by: java.net.ConnectException: Connection refused (Connection refused)
.....
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyService': Invocation of init method failed; nested exception is com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.
Caused by: com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.
如您所见,我传递了环境变量 AWS_DEFAULT_REGION(也尝试过 AWS_REGION),但仍然收到此错误。
我正在使用以下 sdk 版本:
<aws-java-sdk-core.version>1.11.800</aws-java-sdk-core.version>
当我从 inteliJ 启动应用程序时,一切都可以运行 gr8,因为我的配置和凭据是在 .aws 目录中配置的。
也许我需要向外部公开任何端口?
最佳答案
我决定启动一个 EC2 节点并将我的代码部署到那里来测试它。不知道为什么将区域环境粘贴到本地容器时不够。
关于本地容器上的java spring boot应用程序无法访问aws,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62449797/