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