Skip to content

Commit

Permalink
feat: support delete with version
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank-III committed Nov 20, 2024
1 parent fc8f559 commit a08ab74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion core/src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down
13 changes: 11 additions & 2 deletions core/src/services/s3/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,16 +764,20 @@ impl S3Core {
self.send(req).await
}

pub async fn s3_delete_objects(&self, paths: Vec<String>) -> Result<Response<Buffer>> {
pub async fn s3_delete_objects(
&self,
paths: Vec<(String, OpDelete)>,
) -> Result<Response<Buffer>> {
let url = format!("{}/?delete", self.endpoint);

let req = Request::post(&url);

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(),
})
Expand Down Expand Up @@ -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<String>,
}

/// Result of DeleteObjects.
Expand Down Expand Up @@ -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()),
},
],
};
Expand All @@ -1105,6 +1113,7 @@ mod tests {
</Object>
<Object>
<Key>sample2.txt</Key>
<VersionId>11111</VersionId>
</Object>
</Delete>"#
// Cleanup space and new line
Expand Down

0 comments on commit a08ab74

Please sign in to comment.