opendal/services/yandex_disk/
writer.rs1use std::sync::Arc;
19
20use http::Request;
21use http::StatusCode;
22
23use super::core::YandexDiskCore;
24use super::error::parse_error;
25use crate::raw::*;
26use crate::*;
27
28pub type YandexDiskWriters = oio::OneShotWriter<YandexDiskWriter>;
29
30pub struct YandexDiskWriter {
31 core: Arc<YandexDiskCore>,
32 path: String,
33}
34
35impl YandexDiskWriter {
36 pub fn new(core: Arc<YandexDiskCore>, path: String) -> Self {
37 YandexDiskWriter { core, path }
38 }
39}
40
41impl oio::OneShotWrite for YandexDiskWriter {
42 async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
43 self.core.ensure_dir_exists(&self.path).await?;
44
45 let upload_url = self.core.get_upload_url(&self.path).await?;
46
47 let req = Request::put(upload_url)
48 .body(bs)
49 .map_err(new_request_build_error)?;
50
51 let resp = self.core.send(req).await?;
52
53 let status = resp.status();
54
55 match status {
56 StatusCode::CREATED => Ok(Metadata::default()),
57 _ => Err(parse_error(resp)),
58 }
59 }
60}