From 21fdb326a66f6f7c6699b618167ad0cee3feb275 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Tue, 28 Oct 2025 21:33:09 -0300 Subject: [PATCH] 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. --- src/main.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main.rs b/src/main.rs index e2bddc32..1849f04f 100644 --- a/src/main.rs +++ b/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");