From 4bb4c2e5fd7d9fcb6b074724c31b4071fb72761d Mon Sep 17 00:00:00 2001 From: Ho Kim Date: Tue, 10 Sep 2024 04:55:17 +0000 Subject: [PATCH] fix: check bucket existence by creating a dummy file --- src/session.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 0283a46..3fc2631 100644 --- a/src/session.rs +++ b/src/session.rs @@ -50,7 +50,7 @@ impl ObjectStorageSession { .map_err(|error| anyhow!("failed to initialize object storage bucket client: {error}"))? .with_path_style(); - if bucket.get_object("/").await.is_err() { + if check_bucket_exists(&bucket).await { if bucket_create { let config = BucketConfiguration::private(); let response = Bucket::create_with_path_style( @@ -304,6 +304,18 @@ impl SessionTask { } } +async fn check_bucket_exists(bucket: &Bucket) -> bool { + const TEST_FILE: &'static str = "/_sos_bucket_test"; + + match bucket.put_object(TEST_FILE, TEST_FILE.as_bytes()).await { + Ok(_) => { + bucket.delete_object(TEST_FILE).await.ok(); + true + } + Err(_) => false, + } +} + async fn cleanup(bucket: Bucket) -> Result<()> { info!("Cleaning up...");