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

AWS S3 Java 2.X创建Bucket失败:HTTP请求解析400错误

问题原因分析

1. 自定义Endpoint配置缺失关键项

手动实现S3EndpointProvider仅指定URL时,未开启路径风格访问。S3默认采用虚拟主机风格(bucket.s3.amazonaws.com),但自定义Endpoint(如私有存储、MinIO等)通常需要路径风格(endpoint-url/bucket),缺少该配置会导致请求路径解析错误,触发400异常。

2. 非法中文引号

代码中用中文引号(‘’、“”)包裹Access Key、Secret Key和Bucket名称,会导致参数值包含无效字符,引发请求解析失败。

3. Endpoint URL格式错误

若指定的Endpoint URL末尾带有斜杠(如http://example.com/),SDK生成的请求路径会变为//bucketname,不符合S3请求规范,触发解析错误。

解决方案

修复步骤1:启用路径风格访问

构建S3Client时添加serviceConfiguration配置,开启路径风格访问:

S3Client s3Client = S3Client.builder()
        .endpointProvider(new S3EndpointProvider() {
            @Override
            public CompletableFuture<Endpoint> resolveEndpoint(S3EndpointParams endpointParams) {
                return CompletableFuture.completedFuture(Endpoint.builder()
                        .url(URI.create("your-endpoint-url")) // 确保URL无末尾斜杠
                        .build());
            }
        })
        .httpClient(UrlConnectionHttpClient.builder().build())
        .region(Region.US_EAST_1)
        .credentialsProvider(() -> AwsBasicCredentials.create("Access key ID", "Secret Key"))
        // 新增:开启路径风格访问
        .serviceConfiguration(S3Configuration.builder()
                .pathStyleAccessEnabled(true)
                .build())
        .build();

修复步骤2:替换中文引号为英文引号

将所有中文引号替换为英文双引号:

// 正确写法示例
AwsBasicCredentials.create("Access key ID", "Secret Key")
.bucket("bucket name")

修复步骤3:规范Endpoint URL格式

确保Endpoint URL不包含末尾斜杠,例如:

  • 错误:http://your-storage-server:9000/
  • 正确:http://your-storage-server:9000

额外验证项

  • 确认Access Key和Secret Key拥有创建Bucket的权限;
  • 若使用第三方S3兼容存储,确认Region设置与服务要求一致(部分服务要求固定为us-east-1);
  • 检查网络连通性,确保SDK能正常访问指定Endpoint。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 18:55:22