elunez/eladmin · error · BadRequestException

存储桶创建失败,请检查配置或权限。

Error message

存储桶创建失败,请检查配置或权限。

What it means

Thrown by S3StorageServiceImpl.upload when the default bucket does not exist AND the auto-create attempt (createBucket, which waits via S3Waiter.waitUntilBucketExists) returned false / threw a non-BucketOwned S3Exception. So both the head-bucket miss and the create-bucket path failed — typically missing s3:CreateBucket permission, an invalid bucket name, or an endpoint that rejects creation.

Source

Thrown at eladmin-tools/src/main/java/me/zhengjie/service/impl/S3StorageServiceImpl.java:118

                // 删除数据库数据
                s3StorageRepository.deleteById(id);
            } catch (S3Exception e) {
                // 处理 AWS 特定的异常
                log.error("从 S3 删除文件时出错: {}", e.awsErrorDetails().errorMessage(), e);
            }
        }
    }

    @Override
    public S3Storage upload(MultipartFile file) {
        String bucketName = amzS3Config.getDefaultBucket();
        // 检查存储桶是否存在
        if (!bucketExists(bucketName)) {
            log.warn("存储桶 {} 不存在,尝试创建...", bucketName);
            if (createBucket(bucketName)){
                log.info("存储桶 {} 创建成功。", bucketName);
            } else {
                throw new BadRequestException("存储桶创建失败,请检查配置或权限。");
            }
        }
        // 获取文件名
        String originalName = file.getOriginalFilename();
        if (StringUtils.isBlank(originalName)) {
            throw new IllegalArgumentException("文件名不能为空");
        }
        // 生成存储路径和文件名
        String folder = DateUtil.format(new Date(), amzS3Config.getTimeformat());
        String fileName = IdUtil.simpleUUID() + "." + FileUtil.getExtensionName(originalName);
        String filePath = folder + "/" + fileName;
        // 构建上传请求
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .bucket(amzS3Config.getDefaultBucket())
                .key(filePath)
                .build();
        // 创建 S3Storage 实例
        S3Storage s3Storage = new S3Storage();

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Pre-create the bucket in the S3 console / MinIO and re-upload — removes reliance on auto-create.
  2. Grant s3:CreateBucket (and s3:HeadBucket) to the configured credentials if auto-create is desired.
  3. Correct the bucket name in config to valid S3 naming (lowercase, no underscores, 3-63 chars).
  4. Check createBucket logs for the waiter failure or the S3Exception detail (error 97 surfaces that message).
  5. For S3-compatible stores, confirm the endpoint allows bucket creation APIs.

Example fix

// before
if (createBucket(bucketName)){
    log.info("存储桶 {} 创建成功。", bucketName);
} else {
    throw new BadRequestException("存储桶创建失败,请检查配置或权限。");
}

// after: fail fast with the IAM expectation made explicit
if (!bucketExists(bucketName) && !createBucket(bucketName)) {
    throw new BadRequestException("存储桶 " + bucketName + " 创建失败:请确认凭证具有 s3:CreateBucket 权限或先手动创建该桶");
}
Defensive patterns

Strategy: fallback

Validate before calling

// pre-create the bucket (idempotent) before enabling uploads
String bucket = amzS3Config.getDefaultBucket();
if (!bucketExists(bucket) && !createBucket(bucket)) {
    throw new BadRequestException("请先手动创建存储桶 " + bucket);
}

Try / catch

try { s3StorageService.upload(file); } catch (BadRequestException e) { if (e.getMessage().contains("存储桶创建失败")) { /* fallback: create bucket out-of-band, then retry upload once */ } }

Prevention

When it happens

Trigger: POST the S3 upload endpoint with amzS3Config.defaultBucket pointing at a nonexistent bucket while the configured IAM user lacks CreateBucket permission (403), or the bucket name violates S3 naming rules (uppercase, underscore, too long) so createBucket errors; also MinIO/compatible endpoints where the waiter times out.

Common situations: Least-privilege IAM policy with only PutObject/GetObject; typo'd bucket name that is also invalid as a new name; region-restricted create permissions; restricted on-prem S3 gateway that forbids client-side bucket creation.

Related errors


AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14). Data as JSON: /api/errors/3be2a2c8923ca804. Report an issue: GitHub.