You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot连接AWS S3失败:Failed to connect to endpoint问题求助

Fixing "Failed to connect to service endpoint" When Connecting AWS S3 from Local Spring Boot App

Hey there! Let's figure out why you're hitting this connection error when trying to work with AWS S3 in your local Spring Boot app.

What's Causing the Error?

Looking at your error logs, the key issue is this section:

Unable to retrieve the requested metadata (/latest/dynamic/instance-identity/document). Failed to connect to service endpoint: com.amazonaws.SdkClientException: Failed to connect to service endpoint:

The AWS SDK is trying to automatically detect your AWS region by accessing the EC2 Instance Metadata Service (IMDS) — a service that only exists on AWS EC2 instances. Since you're running your app locally (your machine name is DESKTOP-1L8IA4K), this metadata endpoint doesn't exist, so the SDK throws a connection failure.

Solutions to Fix This

1. Explicitly Configure Your AWS Region in the S3 Client

The simplest fix is to tell the SDK exactly which region your S3 bucket is in, instead of letting it guess. Update your AmazonConfig class's s3() bean method to include the region explicitly:

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AmazonConfig {

    @Bean
    public AmazonS3 s3() {
        // Replace with your actual AWS access key and secret key
        BasicAWSCredentials awsCreds = new BasicAWSCredentials("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY");
        return AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                // Replace with your S3 bucket's region (e.g., Regions.EU_WEST_1, Regions.AP_SOUTHEAST_1)
                .withRegion(Regions.US_EAST_1)
                .build();
    }
}

Make sure to replace YOUR_ACCESS_KEY, YOUR_SECRET_KEY, and the region with your actual AWS details.

2. Disable Instance Metadata Region Detection

If you prefer not to hardcode the region in your config, you can disable the automatic metadata detection by adding a configuration property or JVM argument:

  • In application.properties:
    aws.region=us-east-1  # Replace with your bucket's region
    
  • As a JVM argument when starting your app:
    -Daws.region.provider.enabled=false -Daws.region=us-east-1
    

3. Check Network/Proxy Settings

If you're working behind a corporate proxy, it might be blocking the SDK's attempt to reach the metadata endpoint (even though it doesn't exist locally, the proxy could cause a timeout). Ensure your Spring Boot app has access to external networks, or configure the AWS SDK to use your proxy:

// Add this to your S3 client builder if using a proxy
.withClientConfiguration(new ClientConfiguration()
        .withProxyHost("your-proxy-host")
        .withProxyPort(your-proxy-port))

Bonus: Verify AWS Credentials

Double-check that your AWS credentials are correctly set up — either via:

  • The ~/.aws/credentials file on your local machine
  • Environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY)
  • Directly in your Spring config (as shown in solution 1)

内容的提问来源于stack exchange,提问作者Cococrunch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:47:33