feat(storage): add S3 bucket existence check during startup
Introduces an async helper function to verify and create the configured S3 bucket if missing. This ensures the application has a valid storage target before initializing the drive, improving reliability in environments with dynamic bucket provisioning.
This commit is contained in:
parent
d9e2fa4b76
commit
21fdb326a6
1 changed files with 25 additions and 0 deletions
25
src/main.rs
25
src/main.rs
|
|
@ -193,6 +193,31 @@ async fn main() -> std::io::Result<()> {
|
|||
));
|
||||
let tool_api = Arc::new(tools::ToolApi::new());
|
||||
|
||||
use opendal::services::S3;
|
||||
use opendal::Operator;
|
||||
use opendal::ErrorKind;
|
||||
|
||||
async fn ensure_bucket_exists(cfg: &AppConfig) {
|
||||
let builder = S3::default()
|
||||
.endpoint(&cfg.minio.server)
|
||||
.access_key_id(&cfg.minio.access_key)
|
||||
.secret_access_key(&cfg.minio.secret_key)
|
||||
.bucket(&cfg.s3_bucket)
|
||||
.root("/");
|
||||
let op = Operator::new(builder).unwrap().finish();
|
||||
match op.stat("/").await {
|
||||
Ok(_) => info!("Bucket {} exists", cfg.s3_bucket),
|
||||
Err(e) if e.kind() == ErrorKind::NotFound => {
|
||||
if let Err(err) = op.create_dir("/").await {
|
||||
info!("Created bucket {}: {:?}", cfg.s3_bucket, err);
|
||||
}
|
||||
}
|
||||
Err(e) => info!("Bucket check failed: {:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
ensure_bucket_exists(&config).await;
|
||||
|
||||
let drive = init_drive(&config.minio)
|
||||
.await
|
||||
.expect("Failed to initialize Drive");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue