1use std::fmt::Debug;
19use std::sync::Arc;
20
21use http::StatusCode;
22use log::debug;
23
24use super::SWIFT_SCHEME;
25use super::SwiftConfig;
26use super::core::parse_error;
27use super::core::*;
28use super::deleter::SwiftDeleter;
29use super::lister::SwiftLister;
30use super::reader::*;
31use super::writer::SwiftWriter;
32use opendal_core::raw::*;
33use opendal_core::*;
34
35#[doc = include_str!("docs.md")]
38#[doc = include_str!("compatible_services.md")]
39#[derive(Debug, Default)]
40pub struct SwiftBuilder {
41 pub(super) config: SwiftConfig,
42}
43
44impl SwiftBuilder {
45 pub fn endpoint(mut self, endpoint: &str) -> Self {
56 self.config.endpoint = if endpoint.is_empty() {
57 None
58 } else {
59 Some(endpoint.trim_end_matches('/').to_string())
60 };
61 self
62 }
63
64 pub fn container(mut self, container: &str) -> Self {
68 self.config.container = if container.is_empty() {
69 None
70 } else {
71 Some(container.trim_end_matches('/').to_string())
72 };
73 self
74 }
75
76 pub fn root(mut self, root: &str) -> Self {
80 self.config.root = if root.is_empty() {
81 None
82 } else {
83 Some(root.to_string())
84 };
85
86 self
87 }
88
89 pub fn token(mut self, token: &str) -> Self {
93 if !token.is_empty() {
94 self.config.token = Some(token.to_string());
95 }
96 self
97 }
98
99 pub fn temp_url_key(mut self, key: &str) -> Self {
105 if !key.is_empty() {
106 self.config.temp_url_key = Some(key.to_string());
107 }
108 self
109 }
110
111 pub fn temp_url_hash_algorithm(mut self, algo: &str) -> Self {
117 if !algo.is_empty() {
118 self.config.temp_url_hash_algorithm = Some(algo.to_string());
119 }
120 self
121 }
122}
123
124impl Builder for SwiftBuilder {
125 type Config = SwiftConfig;
126
127 fn build(self) -> Result<impl Service> {
129 debug!("backend build started: {:?}", self);
130
131 let root = normalize_root(&self.config.root.unwrap_or_default());
132 debug!("backend use root {root}");
133
134 let endpoint = match self.config.endpoint {
135 Some(endpoint) => {
136 if endpoint.starts_with("http") {
137 endpoint
138 } else {
139 format!("https://{endpoint}")
140 }
141 }
142 None => {
143 return Err(Error::new(
144 ErrorKind::ConfigInvalid,
145 "missing endpoint for Swift",
146 ));
147 }
148 };
149 debug!("backend use endpoint: {}", endpoint);
150
151 let container = match self.config.container {
152 Some(container) => container,
153 None => {
154 return Err(Error::new(
155 ErrorKind::ConfigInvalid,
156 "missing container for Swift",
157 ));
158 }
159 };
160
161 let token = self.config.token.unwrap_or_default();
162 let temp_url_key = self.config.temp_url_key.unwrap_or_default();
163 let has_temp_url_key = !temp_url_key.is_empty();
164 let temp_url_hash_algorithm = match &self.config.temp_url_hash_algorithm {
165 Some(algo) => TempUrlHashAlgorithm::from_str_opt(algo)?,
166 None => TempUrlHashAlgorithm::Sha256,
167 };
168
169 Ok(SwiftBackend {
170 core: Arc::new(SwiftCore {
171 info: ServiceInfo::new(SWIFT_SCHEME, &root, ""),
172 capability: Capability {
173 stat: true,
174 stat_with_if_match: true,
175 stat_with_if_none_match: true,
176 stat_with_if_modified_since: true,
177 stat_with_if_unmodified_since: true,
178
179 read: true,
180 read_with_suffix: true,
181 read_with_if_match: true,
182 read_with_if_none_match: true,
183 read_with_if_modified_since: true,
184 read_with_if_unmodified_since: true,
185
186 write: true,
187 write_can_empty: true,
188 write_can_multi: true,
189 write_multi_min_size: Some(5 * 1024 * 1024),
190 write_multi_max_size: if cfg!(target_pointer_width = "64") {
191 Some(5 * 1024 * 1024 * 1024)
192 } else {
193 Some(usize::MAX)
194 },
195 write_with_content_type: true,
196 write_with_content_disposition: true,
197 write_with_content_encoding: true,
198 write_with_cache_control: true,
199 write_with_user_metadata: true,
200
201 delete: true,
202 delete_max_size: Some(10000),
203
204 copy: true,
205
206 list: true,
207 list_with_recursive: true,
208 list_with_start_after: true,
209
210 presign: has_temp_url_key,
211 presign_stat: has_temp_url_key,
212 presign_read: has_temp_url_key,
213 presign_write: has_temp_url_key,
214
215 shared: true,
216
217 ..Default::default()
218 },
219 root,
220 endpoint,
221 container,
222 token,
223 temp_url_key,
224 temp_url_hash_algorithm,
225 }),
226 })
227 }
228}
229
230#[derive(Debug, Clone)]
232pub struct SwiftBackend {
233 pub(crate) core: Arc<SwiftCore>,
234}
235
236impl Service for SwiftBackend {
237 type Reader = oio::StreamReader<SwiftReader>;
238 type Writer = oio::MultipartWriter<SwiftWriter>;
239 type Lister = oio::PageLister<SwiftLister>;
240 type Deleter = oio::BatchDeleter<SwiftDeleter>;
241 type Copier = oio::OneShotCopier;
242
243 fn info(&self) -> ServiceInfo {
244 self.core.info.clone()
245 }
246
247 fn capability(&self) -> Capability {
248 self.core.capability
249 }
250
251 async fn create_dir(
252 &self,
253 _ctx: &OperationContext,
254 _path: &str,
255 _args: OpCreateDir,
256 ) -> Result<RpCreateDir> {
257 Err(Error::new(
258 ErrorKind::Unsupported,
259 "operation is not supported",
260 ))
261 }
262
263 async fn stat(&self, ctx: &OperationContext, path: &str, args: OpStat) -> Result<RpStat> {
264 let resp = self.core.swift_get_metadata(ctx, path, &args).await?;
265
266 match resp.status() {
267 StatusCode::OK | StatusCode::NO_CONTENT => {
268 let headers = resp.headers();
269 let mut meta = parse_into_metadata(path, headers)?;
270 let user_meta = parse_prefixed_headers(headers, "x-object-meta-");
271 if !user_meta.is_empty() {
272 meta = meta.with_user_metadata(user_meta);
273 }
274
275 Ok(RpStat::new(meta))
276 }
277 _ => Err(parse_error(resp)),
278 }
279 }
280 fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
281 let output: oio::StreamReader<SwiftReader> = {
282 Ok(oio::StreamReader::new(SwiftReader::new(
283 self.clone(),
284 ctx.clone(),
285 path,
286 args,
287 )))
288 }?;
289
290 Ok(output)
291 }
292
293 fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
294 let output: oio::MultipartWriter<SwiftWriter> = {
295 let concurrent = args.concurrent();
296 let writer = SwiftWriter::new(
297 self.core.clone(),
298 ctx.clone(),
299 args.clone(),
300 path.to_string(),
301 );
302 let w = oio::MultipartWriter::new(ctx.executor().clone(), writer, concurrent);
303
304 Ok(w)
305 }?;
306
307 Ok(output)
308 }
309
310 fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
311 let output: oio::BatchDeleter<SwiftDeleter> = {
312 Ok(oio::BatchDeleter::new(
313 SwiftDeleter::new(self.core.clone(), ctx.clone()),
314 self.core.capability.delete_max_size,
315 ))
316 }?;
317
318 Ok(output)
319 }
320
321 fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
322 let output: oio::PageLister<SwiftLister> = {
323 let l = SwiftLister::new(
324 self.core.clone(),
325 ctx.clone(),
326 path.to_string(),
327 args.recursive(),
328 args.limit(),
329 args.start_after().map(String::from),
330 );
331
332 Ok(oio::PageLister::new(l))
333 }?;
334
335 Ok(output)
336 }
337
338 async fn presign(
339 &self,
340 _ctx: &OperationContext,
341 path: &str,
342 args: OpPresign,
343 ) -> Result<RpPresign> {
344 let (expire, op) = args.into_parts();
345
346 let method = match &op {
347 PresignOperation::Stat(_) => http::Method::HEAD,
348 PresignOperation::Read(_, _) => http::Method::GET,
349 PresignOperation::Write(_) => http::Method::PUT,
350 _ => {
351 return Err(Error::new(
352 ErrorKind::Unsupported,
353 "presign operation is not supported",
354 ));
355 }
356 };
357
358 let url = self.core.swift_temp_url(&method, path, expire)?;
359 let uri: http::Uri = url.parse().map_err(|e| {
360 Error::new(ErrorKind::Unexpected, "failed to parse presigned URL").set_source(e)
361 })?;
362
363 Ok(RpPresign::new(PresignedRequest::new(
364 method,
365 uri,
366 http::HeaderMap::new(),
367 )))
368 }
369
370 fn copy(
371 &self,
372 ctx: &OperationContext,
373 from: &str,
374 to: &str,
375 _args: OpCopy,
376 _opts: OpCopier,
377 ) -> Result<Self::Copier> {
378 let core = self.core.clone();
379 let ctx = ctx.clone();
380 let from = from.to_string();
381 let to = to.to_string();
382
383 Ok(oio::OneShotCopier::new(async move {
384 let resp = core.swift_copy(&ctx, &from, &to).await?;
387
388 let status = resp.status();
389
390 match status {
391 StatusCode::CREATED | StatusCode::OK => Ok(Metadata::default()),
392 _ => Err(parse_error(resp)),
393 }
394 }))
395 }
396
397 async fn rename(
398 &self,
399 _ctx: &OperationContext,
400 _from: &str,
401 _to: &str,
402 _args: OpRename,
403 ) -> Result<RpRename> {
404 Err(Error::new(
405 ErrorKind::Unsupported,
406 "operation is not supported",
407 ))
408 }
409}