opendal/services/azfile/
writer.rs1use std::sync::Arc;
19
20use http::StatusCode;
21
22use super::core::AzfileCore;
23use super::error::parse_error;
24use crate::raw::*;
25use crate::*;
26
27pub type AzfileWriters = TwoWays<oio::OneShotWriter<AzfileWriter>, oio::AppendWriter<AzfileWriter>>;
28
29pub struct AzfileWriter {
30 core: Arc<AzfileCore>,
31 op: OpWrite,
32 path: String,
33}
34
35impl AzfileWriter {
36 pub fn new(core: Arc<AzfileCore>, op: OpWrite, path: String) -> Self {
37 AzfileWriter { core, op, path }
38 }
39}
40
41impl oio::OneShotWrite for AzfileWriter {
42 async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
43 let resp = self
44 .core
45 .azfile_create_file(&self.path, bs.len(), &self.op)
46 .await?;
47
48 let status = resp.status();
49 match status {
50 StatusCode::OK | StatusCode::CREATED => {}
51 _ => {
52 return Err(parse_error(resp).with_operation("Backend::azfile_create_file"));
53 }
54 }
55
56 let resp = self
57 .core
58 .azfile_update(&self.path, bs.len() as u64, 0, bs)
59 .await?;
60 let status = resp.status();
61 match status {
62 StatusCode::OK | StatusCode::CREATED => Ok(Metadata::default()),
63 _ => Err(parse_error(resp).with_operation("Backend::azfile_update")),
64 }
65 }
66}
67
68impl oio::AppendWrite for AzfileWriter {
69 async fn offset(&self) -> Result<u64> {
70 let resp = self.core.azfile_get_file_properties(&self.path).await?;
71
72 let status = resp.status();
73
74 match status {
75 StatusCode::OK => Ok(parse_content_length(resp.headers())?.unwrap_or_default()),
76 _ => Err(parse_error(resp)),
77 }
78 }
79
80 async fn append(&self, offset: u64, size: u64, body: Buffer) -> Result<Metadata> {
81 let resp = self
82 .core
83 .azfile_update(&self.path, size, offset, body)
84 .await?;
85
86 let status = resp.status();
87 match status {
88 StatusCode::OK | StatusCode::CREATED => Ok(Metadata::default()),
89 _ => Err(parse_error(resp).with_operation("Backend::azfile_update")),
90 }
91 }
92}