Skip to content

Commit

Permalink
Merge pull request #129 from ZzIsGod1019/zz-object-0612
Browse files Browse the repository at this point in the history
object: support check key exist
  • Loading branch information
4t145 authored Jun 12, 2024
2 parents 47a82de + ac8f57e commit 3316ce3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions tardis/src/os/os_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
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
Expand Down Expand Up @@ -154,6 +159,8 @@ trait TardisOSOperations {

async fn object_get(&self, path: &str, bucket_name: Option<&str>) -> TardisResult<Vec<u8>>;

async fn object_exist(&self, path: &str, bucket_name: Option<&str>) -> TardisResult<bool>;

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<()>;
Expand Down Expand Up @@ -253,6 +260,22 @@ impl TardisOSOperations for TardisOSS3Client {
}
}

async fn object_exist(&self, path: &str, bucket_name: Option<&str>) -> TardisResult<bool> {
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?;
Expand Down
4 changes: 2 additions & 2 deletions tardis/tests/test_os_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))?);
//
Expand Down

0 comments on commit 3316ce3

Please sign in to comment.