opendal/services/azblob/
delete.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use http::StatusCode;
21
22use super::core::*;
23use super::error::parse_error;
24use crate::raw::oio::BatchDeleteResult;
25use crate::raw::*;
26use crate::*;
27
28pub struct AzblobDeleter {
29    core: Arc<AzblobCore>,
30}
31
32impl AzblobDeleter {
33    pub fn new(core: Arc<AzblobCore>) -> Self {
34        Self { core }
35    }
36}
37
38impl oio::BatchDelete for AzblobDeleter {
39    async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
40        let resp = self.core.azblob_delete_blob(&path).await?;
41
42        let status = resp.status();
43
44        match status {
45            StatusCode::ACCEPTED | 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        // TODO: Add remove version support.
52        let paths = batch.into_iter().map(|(p, _)| p).collect::<Vec<_>>();
53
54        // construct and complete batch request
55        let resp = self.core.azblob_batch_delete(&paths).await?;
56
57        // check response status
58        if resp.status() != StatusCode::ACCEPTED {
59            return Err(parse_error(resp));
60        }
61
62        // get boundary from response header
63        let boundary = parse_multipart_boundary(resp.headers())?
64            .ok_or_else(|| {
65                Error::new(
66                    ErrorKind::Unexpected,
67                    "invalid response: no boundary provided in header",
68                )
69            })?
70            .to_string();
71
72        let bs = resp.into_body().to_bytes();
73        let multipart: Multipart<MixedPart> =
74            Multipart::new().with_boundary(&boundary).parse(bs)?;
75        let parts = multipart.into_parts();
76
77        if paths.len() != parts.len() {
78            return Err(Error::new(
79                ErrorKind::Unexpected,
80                "invalid batch response, paths and response parts don't match",
81            ));
82        }
83
84        let mut batched_result = BatchDeleteResult::default();
85
86        for (i, part) in parts.into_iter().enumerate() {
87            let resp = part.into_response();
88            let path = paths[i].clone();
89
90            // deleting not existing objects is ok
91            if resp.status() == StatusCode::ACCEPTED || resp.status() == StatusCode::NOT_FOUND {
92                batched_result.succeeded.push((path, OpDelete::default()));
93            } else {
94                batched_result
95                    .failed
96                    .push((path, OpDelete::default(), parse_error(resp)));
97            }
98        }
99
100        // If no object is deleted, return directly.
101        if batched_result.succeeded.is_empty() {
102            let err = batched_result.failed.remove(0).2;
103            return Err(err);
104        }
105
106        Ok(batched_result)
107    }
108}