diff --git a/tardis/src/os/os_client.rs b/tardis/src/os/os_client.rs index 8082bf9..a46bc8a 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,22 @@ 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()))?); //