use std::fmt::Debug;
use serde::Deserialize;
use serde::Serialize;
use opendal_core::OperatorUri;
use super::backend::OssBuilder;
#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(default)]
#[non_exhaustive]
pub struct OssConfig {
pub root: Option<String>,
pub endpoint: Option<String>,
pub presign_endpoint: Option<String>,
pub bucket: String,
pub addressing_style: Option<String>,
pub presign_addressing_style: Option<String>,
pub enable_versioning: bool,
pub server_side_encryption: Option<String>,
pub server_side_encryption_key_id: Option<String>,
pub allow_anonymous: bool,
pub access_key_id: Option<String>,
pub access_key_secret: Option<String>,
pub security_token: Option<String>,
#[deprecated(
since = "0.52.0",
note = "Please use `delete_max_size` instead of `batch_max_operations`"
)]
pub batch_max_operations: Option<usize>,
pub delete_max_size: Option<usize>,
pub role_arn: Option<String>,
pub role_session_name: Option<String>,
pub oidc_provider_arn: Option<String>,
pub oidc_token_file: Option<String>,
pub sts_endpoint: Option<String>,
}
impl Debug for OssConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Builder")
.field("root", &self.root)
.field("bucket", &self.bucket)
.field("endpoint", &self.endpoint)
.field("allow_anonymous", &self.allow_anonymous)
.finish_non_exhaustive()
}
}
impl opendal_core::Configurator for OssConfig {
type Builder = OssBuilder;
fn from_uri(uri: &OperatorUri) -> opendal_core::Result<Self> {
let mut map = uri.options().clone();
if let Some(name) = uri.name() {
map.insert("bucket".to_string(), name.to_string());
}
if let Some(root) = uri.root() {
map.insert("root".to_string(), root.to_string());
}
Self::from_iter(map)
}
fn into_builder(self) -> Self::Builder {
OssBuilder { config: self }
}
}
#[cfg(test)]
mod tests {
use super::*;
use opendal_core::Configurator;
use opendal_core::OperatorUri;
#[test]
fn from_uri_extracts_bucket_and_root() {
let uri = OperatorUri::new(
"oss://example-bucket/path/to/root",
Vec::<(String, String)>::new(),
)
.unwrap();
let cfg = OssConfig::from_uri(&uri).unwrap();
assert_eq!(cfg.bucket, "example-bucket");
assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
}
}