opendal/services/azblob/
delete.rs1use super::core::*;
19use super::error::parse_error;
20use crate::raw::oio::BatchDeleteResult;
21use crate::raw::*;
22use crate::*;
23use http::StatusCode;
24use std::sync::Arc;
25
26pub struct AzblobDeleter {
27 core: Arc<AzblobCore>,
28}
29
30impl AzblobDeleter {
31 pub fn new(core: Arc<AzblobCore>) -> Self {
32 Self { core }
33 }
34}
35
36impl oio::BatchDelete for AzblobDeleter {
37 async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
38 let resp = self.core.azblob_delete_blob(&path).await?;
39
40 let status = resp.status();
41
42 match status {
43 StatusCode::ACCEPTED | StatusCode::NOT_FOUND => Ok(()),
44 _ => Err(parse_error(resp)),
45 }
46 }
47
48 async fn delete_batch(&self, batch: Vec<(String, OpDelete)>) -> Result<BatchDeleteResult> {
49 let paths = batch.into_iter().map(|(p, _)| p).collect::<Vec<_>>();
51
52 let resp = self.core.azblob_batch_delete(&paths).await?;
54
55 if resp.status() != StatusCode::ACCEPTED {
57 return Err(parse_error(resp));
58 }
59
60 let boundary = parse_multipart_boundary(resp.headers())?
62 .ok_or_else(|| {
63 Error::new(
64 ErrorKind::Unexpected,
65 "invalid response: no boundary provided in header",
66 )
67 })?
68 .to_string();
69
70 let bs = resp.into_body().to_bytes();
71 let multipart: Multipart<MixedPart> =
72 Multipart::new().with_boundary(&boundary).parse(bs)?;
73 let parts = multipart.into_parts();
74
75 if paths.len() != parts.len() {
76 return Err(Error::new(
77 ErrorKind::Unexpected,
78 "invalid batch response, paths and response parts don't match",
79 ));
80 }
81
82 let mut batched_result = BatchDeleteResult::default();
83
84 for (i, part) in parts.into_iter().enumerate() {
85 let resp = part.into_response();
86 let path = paths[i].clone();
87
88 if resp.status() == StatusCode::ACCEPTED || resp.status() == StatusCode::NOT_FOUND {
90 batched_result.succeeded.push((path, OpDelete::default()));
91 } else {
92 batched_result
93 .failed
94 .push((path, OpDelete::default(), parse_error(resp)));
95 }
96 }
97
98 if batched_result.succeeded.is_empty() {
100 let err = batched_result.failed.remove(0).2;
101 return Err(err);
102 }
103
104 Ok(batched_result)
105 }
106}