From a08ab74d91dd77ba4ad0d63b0119345d6a5802eb Mon Sep 17 00:00:00 2001 From: Frank Wang <1454884738@qq.com> Date: Wed, 20 Nov 2024 22:20:45 +0000 Subject: [PATCH] feat: support delete with version --- core/src/services/s3/backend.rs | 5 ++++- core/src/services/s3/core.rs | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/src/services/s3/backend.rs b/core/src/services/s3/backend.rs index 65dca0f449b2..d509bdbc4eee 100644 --- a/core/src/services/s3/backend.rs +++ b/core/src/services/s3/backend.rs @@ -1109,7 +1109,10 @@ impl Access for S3Backend { .with_context("length", ops.len().to_string())); } - let paths = ops.into_iter().map(|(p, _)| p).collect(); + let paths = ops + .into_iter() + .map(|(p, BatchOperation::Delete(del))| (p, del)) + .collect(); let resp = self.core.s3_delete_objects(paths).await?; diff --git a/core/src/services/s3/core.rs b/core/src/services/s3/core.rs index 745f198f67af..b5adcb90daa2 100644 --- a/core/src/services/s3/core.rs +++ b/core/src/services/s3/core.rs @@ -764,7 +764,10 @@ impl S3Core { self.send(req).await } - pub async fn s3_delete_objects(&self, paths: Vec) -> Result> { + pub async fn s3_delete_objects( + &self, + paths: Vec<(String, OpDelete)>, + ) -> Result> { let url = format!("{}/?delete", self.endpoint); let req = Request::post(&url); @@ -772,8 +775,9 @@ impl S3Core { let content = quick_xml::se::to_string(&DeleteObjectsRequest { object: paths .into_iter() - .map(|path| DeleteObjectsRequestObject { + .map(|(path, op)| DeleteObjectsRequestObject { key: build_abs_path(&self.root, &path), + version_id: op.version().map(|v| v.to_owned()), }) .collect(), }) @@ -904,6 +908,8 @@ pub struct DeleteObjectsRequest { #[serde(rename_all = "PascalCase")] pub struct DeleteObjectsRequestObject { pub key: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub version_id: Option, } /// Result of DeleteObjects. @@ -1088,9 +1094,11 @@ mod tests { object: vec![ DeleteObjectsRequestObject { key: "sample1.txt".to_string(), + version_id: None, }, DeleteObjectsRequestObject { key: "sample2.txt".to_string(), + version_id: Some("11111".to_owned()), }, ], }; @@ -1105,6 +1113,7 @@ mod tests { sample2.txt + 11111 "# // Cleanup space and new line