opendal/services/oss/
delete.rs1use super::core::*;
19use super::error::parse_error;
20use crate::raw::oio::BatchDeleteResult;
21use crate::raw::*;
22use crate::*;
23use bytes::Buf;
24use http::StatusCode;
25use std::collections::HashSet;
26use std::sync::Arc;
27
28pub struct OssDeleter {
29 core: Arc<OssCore>,
30}
31
32impl OssDeleter {
33 pub fn new(core: Arc<OssCore>) -> Self {
34 Self { core }
35 }
36}
37
38impl oio::BatchDelete for OssDeleter {
39 async fn delete_once(&self, path: String, args: OpDelete) -> Result<()> {
40 let resp = self.core.oss_delete_object(&path, &args).await?;
41
42 let status = resp.status();
43
44 match status {
45 StatusCode::NO_CONTENT | StatusCode::NOT_FOUND => Ok(()),
46 _ => Err(parse_error(resp)),
47 }
48 }
49
50 async fn delete_batch(&self, batch: Vec<(String, OpDelete)>) -> Result<BatchDeleteResult> {
51 let mut keys: HashSet<(String, OpDelete)> = batch
54 .iter()
55 .map(|path| (path.0.to_owned(), path.1.clone()))
56 .collect();
57
58 let resp = self.core.oss_delete_objects(batch).await?;
59
60 let status = resp.status();
61
62 if status != StatusCode::OK {
63 return Err(parse_error(resp));
64 }
65
66 let bs = resp.into_body();
67
68 let result: DeleteObjectsResult =
69 quick_xml::de::from_reader(bs.reader()).map_err(new_xml_deserialize_error)?;
70
71 if result.deleted.is_empty() {
72 return Err(Error::new(
73 ErrorKind::Unexpected,
74 "oss delete this key failed for reason we don't know",
75 ));
76 }
77
78 let mut batched_result = BatchDeleteResult {
79 succeeded: Vec::with_capacity(result.deleted.len()),
80 failed: Vec::with_capacity(keys.len() - result.deleted.len()),
81 };
82
83 for i in result.deleted {
84 let path = build_rel_path(&self.core.root, &i.key);
85 let mut op = OpDelete::default();
86 if let Some(version) = &i.version_id {
87 op = op.with_version(version);
88 }
89 let object = (path, op);
90 keys.remove(&object);
91 batched_result.succeeded.push(object);
92 }
93 for (path, op) in keys {
95 batched_result.failed.push((
96 path,
97 op,
98 Error::new(
99 ErrorKind::Unexpected,
100 "oss delete this key failed for reason we don't know",
101 ),
102 ));
103 }
104
105 Ok(batched_result)
106 }
107}