opendal_service_tos/
config.rs1use std::fmt::Debug;
19
20use opendal_core::Configurator;
21use opendal_core::OperatorUri;
22use opendal_core::Result;
23use serde::Deserialize;
24use serde::Serialize;
25
26use crate::backend::TosBuilder;
27
28#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
30#[serde(default)]
31#[non_exhaustive]
32pub struct TosConfig {
33 pub root: Option<String>,
39 pub bucket: String,
43 pub endpoint: Option<String>,
52 pub region: Option<String>,
60 #[serde(alias = "tos_access_key_id", alias = "volcengine_access_key_id")]
65 pub access_key_id: Option<String>,
66 #[serde(
71 alias = "tos_secret_access_key",
72 alias = "volcengine_secret_access_key"
73 )]
74 pub secret_access_key: Option<String>,
75 #[serde(alias = "tos_security_token", alias = "volcengine_session_token")]
80 pub security_token: Option<String>,
81 pub disable_config_load: bool,
88 pub skip_signature: bool,
90}
91
92impl Debug for TosConfig {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 f.debug_struct("TosConfig")
95 .field("root", &self.root)
96 .field("bucket", &self.bucket)
97 .field("endpoint", &self.endpoint)
98 .field("region", &self.region)
99 .finish_non_exhaustive()
100 }
101}
102
103impl Configurator for TosConfig {
104 type Builder = TosBuilder;
105
106 fn from_uri(uri: &OperatorUri) -> Result<Self> {
107 let mut map = uri.options().clone();
108
109 if let Some(name) = uri.name() {
110 map.insert("bucket".to_string(), name.to_string());
111 }
112
113 if let Some(root) = uri.root() {
114 map.insert("root".to_string(), root.to_string());
115 }
116
117 Self::from_iter(map)
118 }
119
120 fn into_builder(self) -> Self::Builder {
121 TosBuilder { config: self }
122 }
123}
124
125#[cfg(test)]
126mod tests {
127 use std::iter;
128
129 use super::*;
130 use opendal_core::Configurator;
131 use opendal_core::OperatorUri;
132
133 #[test]
134 fn test_tos_config_original_field_names() {
135 let json = r#"{
136 "bucket": "test-bucket",
137 "access_key_id": "test-key",
138 "secret_access_key": "test-secret",
139 "region": "cn-beijing",
140 "endpoint": "https://tos-cn-beijing.volces.com"
141 }"#;
142
143 let config: TosConfig = serde_json::from_str(json).unwrap();
144 assert_eq!(config.bucket, "test-bucket");
145 assert_eq!(config.access_key_id, Some("test-key".to_string()));
146 assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
147 assert_eq!(config.region, Some("cn-beijing".to_string()));
148 assert_eq!(
149 config.endpoint,
150 Some("https://tos-cn-beijing.volces.com".to_string())
151 );
152 }
153
154 #[test]
155 fn test_tos_config_tos_prefixed_aliases() {
156 let json = r#"{
157 "tos_access_key_id": "test-key",
158 "tos_secret_access_key": "test-secret",
159 "tos_security_token": "test-token"
160 }"#;
161
162 let config: TosConfig = serde_json::from_str(json).unwrap();
163 assert_eq!(config.access_key_id, Some("test-key".to_string()));
164 assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
165 assert_eq!(config.security_token, Some("test-token".to_string()));
166 }
167
168 #[test]
169 fn test_tos_config_volcengine_prefixed_aliases() {
170 let json = r#"{
171 "volcengine_access_key_id": "test-key",
172 "volcengine_secret_access_key": "test-secret",
173 "volcengine_session_token": "test-token"
174 }"#;
175
176 let config: TosConfig = serde_json::from_str(json).unwrap();
177 assert_eq!(config.access_key_id, Some("test-key".to_string()));
178 assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
179 assert_eq!(config.security_token, Some("test-token".to_string()));
180 }
181
182 #[test]
183 fn from_uri_extracts_bucket_and_root() {
184 let uri = OperatorUri::new("tos://example-bucket/path/to/root", iter::empty()).unwrap();
185 let cfg = TosConfig::from_uri(&uri).unwrap();
186 assert_eq!(cfg.bucket, "example-bucket");
187 assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
188 }
189
190 #[test]
191 fn from_uri_extracts_endpoint() {
192 let uri = OperatorUri::new(
193 "tos://example-bucket/path/to/root?endpoint=https%3A%2F%2Fcustom-tos-endpoint.com",
194 iter::empty(),
195 )
196 .unwrap();
197 let cfg = TosConfig::from_uri(&uri).unwrap();
198 assert_eq!(cfg.bucket, "example-bucket");
199 assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
200 assert_eq!(
201 cfg.endpoint.as_deref(),
202 Some("https://custom-tos-endpoint.com")
203 );
204 }
205}