hoslo commented on code in PR #3790: URL: https://github.com/apache/incubator-opendal/pull/3790#discussion_r1433587683
########## core/src/services/upyun/backend.rs: ########## @@ -0,0 +1,377 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use async_trait::async_trait; +use http::StatusCode; +use log::debug; +use serde::Deserialize; +use std::collections::HashMap; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::sync::Arc; + +use super::core::parse_info; +use super::core::UpYunCore; +use super::error::parse_error; +use super::lister::UpYunLister; +use super::writer::UpYunWriter; +use super::writer::UpYunWriters; +use crate::raw::*; +use crate::services::upyun::core::UpYunSigner; +use crate::*; + +/// Config for backblaze upyun services support. +#[derive(Default, Deserialize)] +#[serde(default)] +#[non_exhaustive] +pub struct UpYunConfig { + /// root of this backend. + /// + /// All operations will happen under this root. + pub root: Option<String>, + /// bucket address of this backend. + pub bucket: String, + /// username of this backend. + pub operator: Option<String>, + /// password of this backend. + pub password: Option<String>, +} + +impl Debug for UpYunConfig { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Config"); + + ds.field("root", &self.root); + ds.field("bucket", &self.bucket); + ds.field("operator", &self.operator); + + ds.finish() + } +} + +/// [upyun](https://www.upyun.com/products/file-storage) services support. +#[doc = include_str!("docs.md")] +#[derive(Default)] +pub struct UpYunBuilder { + config: UpYunConfig, + + http_client: Option<HttpClient>, +} + +impl Debug for UpYunBuilder { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("UpYunBuilder"); + + d.field("config", &self.config); + d.finish_non_exhaustive() + } +} + +impl UpYunBuilder { + /// Set root of this backend. + /// + /// All operations will happen under this root. + pub fn root(&mut self, root: &str) -> &mut Self { + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + + self + } + + /// bucket of this backend. + /// + /// It is required. e.g. `test` + pub fn bucket(&mut self, bucket: &str) -> &mut Self { + self.config.bucket = bucket.to_string(); + + self + } + + /// operator of this backend. + /// + /// It is required. e.g. `test` + pub fn operator(&mut self, operator: &str) -> &mut Self { + self.config.operator = if operator.is_empty() { + None + } else { + Some(operator.to_string()) + }; + + self + } + + /// password of this backend. + /// + /// It is required. e.g. `asecret` + pub fn password(&mut self, password: &str) -> &mut Self { + self.config.password = if password.is_empty() { + None + } else { + Some(password.to_string()) + }; + + self + } + + /// Specify the http client that used by this service. + /// + /// # Notes + /// + /// This API is part of OpenDAL's Raw API. `HttpClient` could be changed + /// during minor updates. + pub fn http_client(&mut self, client: HttpClient) -> &mut Self { + self.http_client = Some(client); + self + } +} + +impl Builder for UpYunBuilder { + const SCHEME: Scheme = Scheme::UpYun; + type Accessor = UpYunBackend; + + /// Converts a HashMap into an UpYunBuilder instance. + /// + /// # Arguments + /// + /// * `map` - A HashMap containing the configuration values. + /// + /// # Returns + /// + /// Returns an instance of UpYunBuilder. + fn from_map(map: HashMap<String, String>) -> Self { + // Deserialize the configuration from the HashMap. + let config = UpYunConfig::deserialize(ConfigDeserializer::new(map)) + .expect("config deserialize must succeed"); + + // Create an UpYunBuilder instance with the deserialized config. + UpYunBuilder { + config, + http_client: None, + } + } + + /// Builds the backend and returns the result of UpYunBackend. + fn build(&mut self) -> Result<Self::Accessor> { + debug!("backend build started: {:?}", &self); + + let root = normalize_root(&self.config.root.clone().unwrap_or_default()); + debug!("backend use root {}", &root); + + // Handle bucket. + if self.config.bucket.is_empty() { + return Err(Error::new(ErrorKind::ConfigInvalid, "bucket is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::UpYun)); + } + + debug!("backend use bucket {}", &self.config.bucket); + + let operator = match &self.config.operator { + Some(operator) => Ok(operator.clone()), + None => Err(Error::new(ErrorKind::ConfigInvalid, "operator is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::UpYun)), + }?; + + let password = match &self.config.password { + Some(password) => Ok(password.clone()), + None => Err(Error::new(ErrorKind::ConfigInvalid, "password is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::UpYun)), + }?; + + let client = if let Some(client) = self.http_client.take() { + client + } else { + HttpClient::new().map_err(|err| { + err.with_operation("Builder::build") + .with_context("service", Scheme::UpYun) + })? + }; + + let signer = UpYunSigner { + operator: operator.clone(), + password: password.clone(), + }; + + Ok(UpYunBackend { + core: Arc::new(UpYunCore { + root, + operator, + password, + bucket: self.config.bucket.clone(), + signer, + client, + }), + }) + } +} + +/// Backend for upyun services. +#[derive(Debug, Clone)] +pub struct UpYunBackend { + core: Arc<UpYunCore>, +} + +#[async_trait] +impl Accessor for UpYunBackend { + type Reader = IncomingAsyncBody; + + type BlockingReader = (); + + type Writer = UpYunWriters; + + type BlockingWriter = (); + + type Lister = oio::PageLister<UpYunLister>; + + type BlockingLister = (); + + fn info(&self) -> AccessorInfo { + let mut am = AccessorInfo::default(); + am.set_scheme(Scheme::UpYun) + .set_root(&self.core.root) + .set_native_capability(Capability { + stat: true, + + create_dir: true, + + read: true, + read_can_next: true, + + write: true, + write_can_empty: true, + write_can_multi: true, + write_with_cache_control: true, + write_with_content_type: true, + + write_multi_min_size: Some(1024 * 1024), + write_multi_max_size: Some(50 * 1024 * 1024), Review Comment: https://help.upyun.com/knowledge-base/rest_api/#e5b9b6e8a18ce5bc8fe696ade782b9e7bbade4bca0 > 文件分块:数据传输时,把文件切分成小块进行上传,分块大小为1M的整数倍,默认1M,最大50M,最后一个分块例外。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
