{"record":{"id":"59a555f99559e3e8","repo":"elunez/eladmin","slug":"error-59a555","errorCode":null,"errorMessage":"创建存储桶时出错: {}","messagePattern":"创建存储桶时出错: (.+?)","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"eladmin-tools/src/main/java/me/zhengjie/service/impl/S3StorageServiceImpl.java","lineNumber":260,"sourceCode":"            S3Waiter s3Waiter = s3Client.waiter();\n            CreateBucketRequest bucketRequest = CreateBucketRequest.builder()\n                    .bucket(bucketName)\n                    .acl(BucketCannedACL.PRIVATE)\n                    .build();\n            s3Client.createBucket(bucketRequest);\n            // 等待直到存储桶创建完成\n            HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder()\n                    .bucket(bucketName)\n                    .build();\n            // 使用 WaiterResponse 等待存储桶存在\n            WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);\n            waiterResponse.matched().response().ifPresent(response ->\n                    log.info(\"存储桶 '{}' 创建成功，状态: {}\", bucketName, response.sdkHttpResponse().statusCode())\n            );\n        } catch (BucketAlreadyOwnedByYouException e) {\n            log.warn(\"存储桶 '{}' 已经被您拥有，无需重复创建。\", bucketName);\n        } catch (S3Exception e) {\n            throw new BadRequestException(\"创建存储桶时出错: \" + e.awsErrorDetails().errorMessage());\n        }\n        return true;\n    }\n}","sourceCodeStart":242,"sourceCodeEnd":264,"githubUrl":"https://github.com/elunez/eladmin/blob/55fbf705956949697dbd68bf9003776609d3d029/eladmin-tools/src/main/java/me/zhengjie/service/impl/S3StorageServiceImpl.java#L242-L264","documentation":"BadRequestException thrown from createBucket(bucketName) when the bucket-creation flow (CreateBucket + S3Waiter.waitUntilBucketExists) fails with an S3Exception other than BucketAlreadyOwnedByYou. Typical appended details: AccessDenied (no s3:CreateBucket), BucketAlreadyExists (name owned by another account globally), InvalidBucketName, or IllegalLocationConstraint for a wrong-region create.","triggerScenarios":"First upload to a configured-but-missing bucket (see error 91's call site) while: the IAM principal lacks CreateBucket; the name is globally taken (S3 namespace is worldwide); the client's region doesn't match the bucket-location constraint; or a compatible store rejects the create.","commonSituations":"Least-privilege keys; choosing a generic bucket name like 'my-files' that someone else owns; MinIO endpoints that require a specific LocationConstraint; waiter timing out and surfacing a failure response as S3Exception.","solutions":["Pre-create the bucket manually in the console/MinIO so upload never needs auto-create.","Read the appended AWS message: AccessDenied -> grant s3:CreateBucket; BucketAlreadyExists -> pick a unique name and update config.","Ensure the S3Client's region matches where the bucket may be created (region constraint mismatch raises IllegalLocationConstraint).","For MinIO, follow its bucket-create semantics (region 'us-east-1' default).","Re-run the upload after fixing; createBucket returning true proceeds normally."],"exampleFix":"// before\n} catch (S3Exception e) {\n    throw new BadRequestException(\"创建存储桶时出错: \" + e.awsErrorDetails().errorMessage());\n}\nreturn true;\n\n// after: treat 'already exists elsewhere' distinctly\ncatch (S3Exception e) {\n    if (e.statusCode() == 409) {\n        throw new BadRequestException(\"存储桶名已被其他账户占用，请更换桶名\");\n    }\n    throw new BadRequestException(\"创建存储桶时出错: \" + e.awsErrorDetails().errorMessage());\n}\nreturn true;","handlingStrategy":"fallback","validationCode":"// prefer explicit pre-creation; only auto-create with a proven-valid name\nString bucket = amzS3Config.getDefaultBucket();\nif (!bucket.matches(\"^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$\")) {\n    throw new BadRequestException(\"存储桶名称不符合 S3 规范: \" + bucket);\n}","typeGuard":null,"tryCatchPattern":"try { s3StorageService.upload(file); } catch (BadRequestException e) { if (e.getMessage().startsWith(\"创建存储桶时出错\")) { /* fallback: admin creates bucket manually, then retry the upload once */ } }","preventionTips":["Create buckets as a provisioning step; treat upload-time creation as a convenience, not the plan.","Choose globally unique bucket names — S3's namespace is shared across all accounts.","Match client region to the intended bucket location constraint."],"tags":["s3","bucket-creation","aws-sdk-v2","iam","region"],"backgroundTag":null,"analyzedSha":"55fbf705956949697dbd68bf9003776609d3d029","analyzedAt":"2026-08-14T11:56:12.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}