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

按Quarkus文档配置S3连接失败,AWS CLI可正常连接求助

Quarkus S3 上传提示 NoSuchBucketException 但 AWS CLI 正常

问题描述

按照 Quarkus 官方文档配置 S3 服务后,调用上传 API 始终返回 NoSuchBucketException,但通过 AWS CLI 可以正常向目标存储桶上传文件。确认存储桶存在且 .aws/credentials 配置正确。

资源类代码

@Path("/s3")
public class S3SyncClientResource extends CommonResource {
    @Inject
    S3Client s3;

    @POST
    @Path("upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(FormData formData) throws Exception {

        if (formData.filename == null || formData.filename.isEmpty()) {
            return Response.status(Status.BAD_REQUEST).build();
        }

        if (formData.mimetype == null || formData.mimetype.isEmpty()) {
            return Response.status(Status.BAD_REQUEST).build();
        }

        PutObjectResponse putResponse = s3.putObject(buildPutRequest(formData),
                RequestBody.fromFile(formData.data));
        if (putResponse != null) {
            return Response.ok().status(Status.CREATED).build();
        } else {
            return Response.serverError().build();
        }
    }

    @GET
    @Path("download/{objectKey}")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response downloadFile(String objectKey) {
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(buildGetRequest(objectKey));
        ResponseBuilder response = Response.ok(objectBytes.asUtf8String());
        response.header("Content-Disposition", "attachment;filename=" + objectKey);
        response.header("Content-Type", objectBytes.response().contentType());
        return response.build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<FileObject> listFiles() {
        ListObjectsRequest listRequest = ListObjectsRequest.builder().bucket(bucketName).build();

        //HEAD S3 objects to get metadata
        return s3.listObjects(listRequest).contents().stream()
                .map(FileObject::from)
                .sorted(Comparator.comparing(FileObject::getObjectKey))
                .collect(Collectors.toList());
    }
}

abstract public class CommonResource {
    @ConfigProperty(name = "bucket.name")
    String bucketName;

    protected PutObjectRequest buildPutRequest(FormData formData) {
        return PutObjectRequest.builder()
                .bucket(bucketName)
                .key(formData.filename)
                .contentType(formData.mimetype)
                .build();
    }

    protected GetObjectRequest buildGetRequest(String objectKey) {
        return GetObjectRequest.builder()
                .bucket(bucketName)
                .key(objectKey)
                .build();
    }
}

application.properties 配置

quarkus.s3.aws.region=ap-northeast-2
quarkus.s3.aws.credentials.type=default
quarkus.s3.aws.credentials.type=default

pom.xml 依赖

<dependencies>
<dependency>
  <groupId>software.amazon.awssdk</groupId>
  <artifactId>url-connection-client</artifactId>
</dependency>
<dependency>
  <groupId>io.quarkiverse.amazonservices</groupId>
  <artifactId>quarkus-amazon-s3</artifactId>
</dependency>
<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-junit5</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>rest-assured</artifactId>
  <scope>test</scope>
</dependency>
</dependencies>

错误信息

software.amazon.awssdk.services.s3.model.NoSuchBucketException: The specified bucket does not exist (Service: S3, Status Code: 404, Request ID: d436fe05-9929-434a-94ed-42e5f50c5a0f, Extended Request ID: s9lzHYrFp76ZVxRcpX9+5cjAnEH2ROuNkd2BHfIa6UkFVdtjf5mKR3/eTPFvsiP/XV/VLi31234=)

排查步骤与解决方案

  • 补全缺失的存储桶名称配置
    你的 application.properties 中没有配置 bucket.name,但代码通过 @ConfigProperty(name = "bucket.name") 读取存储桶名,这会导致 bucketName 为空或未正确加载。添加以下配置:

    bucket.name=你的存储桶名称
    
  • 确认存储桶与配置区域一致
    检查存储桶是否确实创建在 ap-northeast-2(首尔)区域。S3 存储桶区域不匹配会导致 404,而 AWS CLI 会自动识别存储桶所在区域,因此能正常操作。

  • 移除重复的配置项
    application.properties 中重复定义了 quarkus.s3.aws.credentials.type=default,删除其中一条,避免潜在配置冲突:

    quarkus.s3.aws.region=ap-northeast-2
    quarkus.s3.aws.credentials.type=default
    bucket.name=你的存储桶名称
    
  • 启用路径样式访问(可选)
    若存储桶名称包含点号或使用了旧区域规则,添加路径样式访问配置:

    quarkus.s3.path-style-access=true
    
  • 验证配置加载与凭证信息
    在 CommonResource 中添加初始化代码,确认存储桶名称和凭证是否正确加载:

    @Inject
    AwsCredentialsProvider credentialsProvider;
    
    @PostConstruct
    void init() {
        System.out.println("Loaded bucket name: " + bucketName);
        AwsCredentials credentials = credentialsProvider.resolveCredentials();
        System.out.println("Using AWS access key: " + credentials.accessKeyId());
    }
    

    启动应用后查看控制台输出,确保存储桶名正确,且 Access Key 与 CLI 使用的一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 09:25:59