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 type Composer = ();
243
244 fn info(&self) -> ServiceInfo {
245 self.core.info.clone()
246 }
247
248 fn capability(&self) -> Capability {
249 self.core.capability
250 }
251
252 async fn create_dir(
253 &self,
254 _ctx: &OperationContext,
255 _path: &str,
256 _args: OpCreateDir,
257 ) -> Result<RpCreateDir> {
258 Err(Error::new(
259 ErrorKind::Unsupported,
260 "operation is not supported",
261 ))
262 }
263
264 async fn stat(&self, ctx: &OperationContext, path: &str, args: OpStat) -> Result<RpStat> {
265 let resp = self.core.swift_get_metadata(ctx, path, &args).await?;
266
267 match resp.status() {
268 StatusCode::OK | StatusCode::NO_CONTENT => {
269 let headers = resp.headers();
270 let mut meta = parse_into_metadata(path, headers)?.into_builder();
271 let user_meta = parse_prefixed_headers(headers, "x-object-meta-");
272 if !user_meta.is_empty() {
273 meta.user_metadata(user_meta);
274 }
275
276 Ok(RpStat::new(meta.build()))
277 }
278 _ => Err(parse_error(
279 ErrorContext::new(ServiceOperation("ShowObjectMetadata")),
280 resp,
281 )),
282 }
283 }
284 fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
285 let output: oio::StreamReader<SwiftReader> = {
286 Ok(oio::StreamReader::new(SwiftReader::new(
287 self.clone(),
288 ctx.clone(),
289 path,
290 args,
291 )))
292 }?;
293
294 Ok(output)
295 }
296
297 fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
298 let output: oio::MultipartWriter<SwiftWriter> = {
299 let concurrent = args.concurrent();
300 let writer = SwiftWriter::new(
301 self.core.clone(),
302 ctx.clone(),
303 args.clone(),
304 path.to_string(),
305 );
306 let w = oio::MultipartWriter::new(ctx.executor().clone(), writer, concurrent);
307
308 Ok(w)
309 }?;
310
311 Ok(output)
312 }
313
314 fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
315 let output: oio::BatchDeleter<SwiftDeleter> = {
316 Ok(oio::BatchDeleter::new(
317 SwiftDeleter::new(self.core.clone(), ctx.clone()),
318 self.core.capability.delete_max_size,
319 ))
320 }?;
321
322 Ok(output)
323 }
324
325 fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
326 let output: oio::PageLister<SwiftLister> = {
327 let l = SwiftLister::new(
328 self.core.clone(),
329 ctx.clone(),
330 path.to_string(),
331 args.recursive(),
332 args.limit(),
333 args.start_after().map(String::from),
334 );
335
336 Ok(oio::PageLister::new(l))
337 }?;
338
339 Ok(output)
340 }
341
342 async fn presign(
343 &self,
344 _ctx: &OperationContext,
345 path: &str,
346 args: OpPresign,
347 ) -> Result<RpPresign> {
348 let (expire, op) = args.into_parts();
349
350 let method = match &op {
351 PresignOperation::Stat(_) => http::Method::HEAD,
352 PresignOperation::Read(_, _) => http::Method::GET,
353 PresignOperation::Write(_) => http::Method::PUT,
354 _ => {
355 return Err(Error::new(
356 ErrorKind::Unsupported,
357 "presign operation is not supported",
358 ));
359 }
360 };
361
362 let url = self.core.swift_temp_url(&method, path, expire)?;
363 let uri: http::Uri = url.parse().map_err(|e| {
364 Error::new(ErrorKind::Unexpected, "failed to parse presigned URL").set_source(e)
365 })?;
366
367 Ok(RpPresign::new(PresignedRequest::new(
368 method,
369 uri,
370 http::HeaderMap::new(),
371 )))
372 }
373
374 fn copy(
375 &self,
376 ctx: &OperationContext,
377 from: &str,
378 to: &str,
379 args: OpCopy,
380 ) -> Result<Self::Copier> {
381 let core = self.core.clone();
382 let ctx = ctx.clone();
383 let from = from.to_string();
384 let to = to.to_string();
385
386 Ok(oio::OneShotCopier::new(async move {
387 let source_size = match args.source_content_length_hint() {
388 Some(size) => size,
389 None => {
390 let stat_args: OpStat = options::StatOptions {
391 version: args.source_version().map(str::to_owned),
392 ..Default::default()
393 }
394 .into();
395 let resp = core.swift_get_metadata(&ctx, &from, &stat_args).await?;
396 match resp.status() {
397 StatusCode::OK | StatusCode::NO_CONTENT => {
398 parse_into_metadata(&from, resp.headers())?.content_length()
399 }
400 _ => {
401 return Err(parse_error(
402 ErrorContext::new(ServiceOperation("ShowObjectMetadata")),
403 resp,
404 ));
405 }
406 }
407 }
408 };
409 let resp = core.swift_copy(&ctx, &from, &to).await?;
412
413 let status = resp.status();
414
415 match status {
416 StatusCode::CREATED | StatusCode::OK => {
417 Ok(MetadataBuilder::file(source_size).build())
418 }
419 _ => Err(parse_error(
420 ErrorContext::new(ServiceOperation("CopyObject")),
421 resp,
422 )),
423 }
424 }))
425 }
426
427 async fn rename(
428 &self,
429 _ctx: &OperationContext,
430 _from: &str,
431 _to: &str,
432 _args: OpRename,
433 ) -> Result<RpRename> {
434 Err(Error::new(
435 ErrorKind::Unsupported,
436 "operation is not supported",
437 ))
438 }
439}