opendal/services/gcs/
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 GcsDeleter {
29    core: Arc<GcsCore>,
30}
31
32impl GcsDeleter {
33    pub fn new(core: Arc<GcsCore>) -> Self {
34        Self { core }
35    }
36}
37
38impl oio::BatchDelete for GcsDeleter {
39    async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
40        let resp = self.core.gcs_delete_object(&path).await?;
41
42        // deleting not existing objects is ok
43        if resp.status().is_success() || resp.status() == StatusCode::NOT_FOUND {
44            Ok(())
45        } else {
46            Err(parse_error(resp))
47        }
48    }
49
50    async fn delete_batch(&self, batch: Vec<(String, OpDelete)>) -> Result<BatchDeleteResult> {
51        let paths: Vec<String> = batch.into_iter().map(|(p, _)| p).collect();
52        let resp = self.core.gcs_delete_objects(paths.clone()).await?;
53
54        let status = resp.status();
55
56        // If the overall request isn't formatted correctly and Cloud Storage is unable to parse it into sub-requests, you receive a 400 error.
57        // Otherwise, Cloud Storage returns a 200 status code, even if some or all of the sub-requests fail.
58        if status != StatusCode::OK {
59            return Err(parse_error(resp));
60        }
61
62        let boundary = parse_multipart_boundary(resp.headers())?.ok_or_else(|| {
63            Error::new(
64                ErrorKind::Unexpected,
65                "gcs batch delete response content type is empty",
66            )
67        })?;
68        let multipart: Multipart<MixedPart> = Multipart::new()
69            .with_boundary(boundary)
70            .parse(resp.into_body().to_bytes())?;
71        let parts = multipart.into_parts();
72
73        let mut batched_result = BatchDeleteResult::default();
74
75        for (i, part) in parts.into_iter().enumerate() {
76            let resp = part.into_response();
77            // TODO: maybe we can take it directly?
78            let path = paths[i].clone();
79
80            // deleting not existing objects is ok
81            if resp.status().is_success() || resp.status() == StatusCode::NOT_FOUND {
82                batched_result.succeeded.push((path, OpDelete::default()));
83            } else {
84                batched_result
85                    .failed
86                    .push((path, OpDelete::default(), parse_error(resp)));
87            }
88        }
89
90        // If no object is deleted, return directly.
91        if batched_result.succeeded.is_empty() {
92            let err = batched_result.failed.remove(0).2;
93            return Err(err);
94        }
95
96        Ok(batched_result)
97    }
98}