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