opendal/services/swift/
writer.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::SwiftCore;
23use super::error::parse_error;
24use crate::raw::*;
25use crate::*;
26
27pub struct SwiftWriter {
28    core: Arc<SwiftCore>,
29    op: OpWrite,
30    path: String,
31}
32
33impl SwiftWriter {
34    pub fn new(core: Arc<SwiftCore>, op: OpWrite, path: String) -> Self {
35        SwiftWriter { core, op, path }
36    }
37
38    fn parse_metadata(headers: &http::HeaderMap) -> Result<Metadata> {
39        let mut metadata = Metadata::default();
40
41        if let Some(etag) = parse_etag(headers)? {
42            metadata.set_etag(etag);
43        }
44
45        if let Some(last_modified) = parse_last_modified(headers)? {
46            metadata.set_last_modified(last_modified);
47        }
48
49        Ok(metadata)
50    }
51}
52
53impl oio::OneShotWrite for SwiftWriter {
54    async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
55        let resp = self
56            .core
57            .swift_create_object(&self.path, bs.len() as u64, &self.op, bs)
58            .await?;
59
60        let status = resp.status();
61
62        match status {
63            StatusCode::CREATED | StatusCode::OK => {
64                let metadata = SwiftWriter::parse_metadata(resp.headers())?;
65                Ok(metadata)
66            }
67            _ => Err(parse_error(resp)),
68        }
69    }
70}