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.
1718use super::core::*;
19use super::error::{parse_error, parse_s3_error_code};
20use crate::raw::oio::BatchDeleteResult;
21use crate::raw::*;
22use crate::*;
23use bytes::Buf;
24use http::StatusCode;
25use std::sync::Arc;
2627pub struct S3Deleter {
28 core: Arc<S3Core>,
29}
3031impl S3Deleter {
32pub fn new(core: Arc<S3Core>) -> Self {
33Self { core }
34 }
35}
3637impl oio::BatchDelete for S3Deleter {
38async fn delete_once(&self, path: String, args: OpDelete) -> Result<()> {
39// This would delete the bucket, do not perform
40if self.core.root == "/" && path == "/" {
41return Ok(());
42 }
4344let resp = self.core.s3_delete_object(&path, &args).await?;
4546let status = resp.status();
4748match status {
49 StatusCode::NO_CONTENT => Ok(()),
50// Allow 404 when deleting a non-existing object
51 // This is not a standard behavior, only some s3 alike service like GCS XML API do this.
52 // ref: <https://cloud.google.com/storage/docs/xml-api/delete-object>
53StatusCode::NOT_FOUND => Ok(()),
54_ => Err(parse_error(resp)),
55 }
56 }
5758async fn delete_batch(&self, batch: Vec<(String, OpDelete)>) -> Result<BatchDeleteResult> {
59let resp = self.core.s3_delete_objects(batch).await?;
6061let status = resp.status();
62if status != StatusCode::OK {
63return Err(parse_error(resp));
64 }
6566let bs = resp.into_body();
6768let mut result: DeleteObjectsResult =
69 quick_xml::de::from_reader(bs.reader()).map_err(new_xml_deserialize_error)?;
7071// If no object is deleted, return directly.
72if result.deleted.is_empty() {
73let err = result.error.remove(0);
74return Err(parse_delete_objects_result_error(err));
75 }
7677let mut batched_result = BatchDeleteResult {
78 succeeded: Vec::with_capacity(result.deleted.len()),
79 failed: Vec::with_capacity(result.error.len()),
80 };
81for i in result.deleted {
82let path = build_rel_path(&self.core.root, &i.key);
83let mut op = OpDelete::new();
84if let Some(version_id) = i.version_id {
85 op = op.with_version(version_id.as_str());
86 }
87 batched_result.succeeded.push((path, op));
88 }
89for i in result.error {
90let path = build_rel_path(&self.core.root, &i.key);
91let mut op = OpDelete::new();
92if let Some(version_id) = &i.version_id {
93 op = op.with_version(version_id.as_str());
94 }
95 batched_result
96 .failed
97 .push((path, op, parse_delete_objects_result_error(i)));
98 }
99100Ok(batched_result)
101 }
102}
103104fn parse_delete_objects_result_error(err: DeleteObjectsResultError) -> Error {
105let (kind, retryable) =
106 parse_s3_error_code(err.code.as_str()).unwrap_or((ErrorKind::Unexpected, false));
107let mut err: Error = Error::new(kind, format!("{err:?}"));
108if retryable {
109 err = err.set_temporary();
110 }
111 err
112}