From db5288387aeec82421b2c55264420bec04a76b3c Mon Sep 17 00:00:00 2001 From: ZzIsGod1019 <1498852723@qq.com> Date: Wed, 12 Jun 2024 14:29:39 +0800 Subject: [PATCH 1/2] object: support check key exist --- tardis/src/os/os_client.rs | 27 +++++++++++++++++++++++++++ tardis/tests/test_os_client.rs | 4 ++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/tardis/src/os/os_client.rs b/tardis/src/os/os_client.rs index 8082bf9..4eb27a2 100644 --- a/tardis/src/os/os_client.rs +++ b/tardis/src/os/os_client.rs @@ -98,6 +98,11 @@ impl TardisOSClient { self.get_client().object_get(path, bucket_name).await } + pub async fn object_exist(&self, path: &str, bucket_name: Option<&str>) -> TardisResult { + trace!("[Tardis.OSClient] Head object {}", path); + self.get_client().object_exist(path, bucket_name).await + } + pub async fn object_delete(&self, path: &str, bucket_name: Option<&str>) -> TardisResult<()> { trace!("[Tardis.OSClient] Deleting object {}", path); self.get_client().object_delete(path, bucket_name).await @@ -154,6 +159,8 @@ trait TardisOSOperations { async fn object_get(&self, path: &str, bucket_name: Option<&str>) -> TardisResult>; + async fn object_exist(&self, path: &str, bucket_name: Option<&str>) -> TardisResult; + async fn object_delete(&self, path: &str, bucket_name: Option<&str>) -> TardisResult<()>; async fn object_copy(&self, from: &str, to: &str, bucket_name: Option<&str>) -> TardisResult<()>; @@ -253,6 +260,26 @@ impl TardisOSOperations for TardisOSS3Client { } } + async fn object_exist(&self, path: &str, bucket_name: Option<&str>) -> TardisResult { + let bucket = self.get_bucket(bucket_name)?; + let result = bucket.head_object(path).await.map_err(TardisError::from); + if result.is_ok() { + Ok(true) + } else if result.clone().expect_err("").code == "404" { + Ok(false) + } else { + Err(TardisError::custom( + &result.expect_err("").code.clone(), + &format!( + "[Tardis.OSClient] Failed to head object {}:{}", + bucket.name, + path + ), + "-1-tardis-os-get-object-error", + )) + } + } + async fn object_delete(&self, path: &str, bucket_name: Option<&str>) -> TardisResult<()> { let bucket = self.get_bucket(bucket_name)?; let response_data = bucket.delete_object(path).await?; diff --git a/tardis/tests/test_os_client.rs b/tardis/tests/test_os_client.rs index 44db434..13883ed 100644 --- a/tardis/tests/test_os_client.rs +++ b/tardis/tests/test_os_client.rs @@ -29,9 +29,9 @@ async fn test_os_client() -> TardisResult<()> { let data = TardisFuns::os().object_get("test/test_cp.txt", Some(bucket_name)).await?; assert_eq!(String::from_utf8(data).unwrap(), "I want to go to S3 测试"); - info!("object_get_url = {}", TardisFuns::os().object_get_url("test/test.txt", 60, Some(bucket_name)).await?); + info!("object_get_url = {:?}", TardisFuns::os().object_exist("test/test.txt", Some(bucket_name)).await?); - info!("object_create_url = {}", TardisFuns::os().object_create_url("test/test1.txt", 60, Some(bucket_name)).await?); + info!("object_create_url = {:?}", TardisFuns::os().object_exist("test/test1.txt", Some(bucket_name)).await?); //info!("object_create_url = {}", TardisFuns::os().object_create_url("test/test2.txt", 1, Some(bucket_name.clone()))?); // From ac8f57ef08350850203f5d829a673b556c2ef514 Mon Sep 17 00:00:00 2001 From: ZzIsGod1019 <1498852723@qq.com> Date: Wed, 12 Jun 2024 14:34:54 +0800 Subject: [PATCH 2/2] fmt --- tardis/src/os/os_client.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tardis/src/os/os_client.rs b/tardis/src/os/os_client.rs index 4eb27a2..a46bc8a 100644 --- a/tardis/src/os/os_client.rs +++ b/tardis/src/os/os_client.rs @@ -102,7 +102,7 @@ impl TardisOSClient { trace!("[Tardis.OSClient] Head object {}", path); self.get_client().object_exist(path, bucket_name).await } - + pub async fn object_delete(&self, path: &str, bucket_name: Option<&str>) -> TardisResult<()> { trace!("[Tardis.OSClient] Deleting object {}", path); self.get_client().object_delete(path, bucket_name).await @@ -270,11 +270,7 @@ impl TardisOSOperations for TardisOSS3Client { } else { Err(TardisError::custom( &result.expect_err("").code.clone(), - &format!( - "[Tardis.OSClient] Failed to head object {}:{}", - bucket.name, - path - ), + &format!("[Tardis.OSClient] Failed to head object {}:{}", bucket.name, path), "-1-tardis-os-get-object-error", )) }