opendal_core/blocking/operator.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::time::Duration;
19
20use tokio::runtime::Handle;
21
22use crate::Operator as AsyncOperator;
23use crate::raw::PresignedRequest;
24use crate::types::IntoOperatorUri;
25use crate::*;
26
27/// Use OpenDAL in blocking context.
28///
29/// # Notes
30///
31/// blocking::Operator is a wrapper around [`AsyncOperator`]. It calls async runtimes' `block_on` API to spawn blocking tasks.
32/// Please avoid using blocking::Operator in async context.
33///
34/// # Examples
35///
36/// ## Init in async context
37///
38/// blocking::Operator will use current async context's runtime to handle the async calls.
39///
40/// This is just for initialization. You must use `blocking::Operator` in blocking context.
41///
42/// ```rust,no_run
43/// # use opendal_core::services;
44/// # use opendal_core::blocking;
45/// # use opendal_core::Operator;
46/// # use opendal_core::Result;
47///
48/// #[tokio::main]
49/// async fn main() -> Result<()> {
50/// // Create fs backend builder.
51/// let builder = services::Memory::default();
52/// let op = Operator::new(builder)?.finish();
53///
54/// // Build an `blocking::Operator` with blocking layer to start operating the storage.
55/// let _: blocking::Operator = blocking::Operator::new(op)?;
56///
57/// Ok(())
58/// }
59/// ```
60///
61/// ## In async context with blocking functions
62///
63/// If `blocking::Operator` is called in blocking function, please fetch a [`tokio::runtime::EnterGuard`]
64/// first. You can use [`Handle::try_current`] first to get the handle and then call [`Handle::enter`].
65/// This often happens in the case that async function calls blocking function.
66///
67/// ```rust,no_run
68/// # use opendal_core::services;
69/// # use opendal_core::blocking;
70/// # use opendal_core::Operator;
71/// # use opendal_core::Result;
72///
73/// #[tokio::main]
74/// async fn main() -> Result<()> {
75/// let _ = blocking_fn()?;
76/// Ok(())
77/// }
78///
79/// fn blocking_fn() -> Result<blocking::Operator> {
80/// // Create fs backend builder.
81/// let builder = services::Memory::default();
82/// let op = Operator::new(builder)?.finish();
83///
84/// let handle = tokio::runtime::Handle::try_current().unwrap();
85/// let _guard = handle.enter();
86/// // Build an `blocking::Operator` to start operating the storage.
87/// let op: blocking::Operator = blocking::Operator::new(op)?;
88/// Ok(op)
89/// }
90/// ```
91///
92/// ## In blocking context
93///
94/// In a pure blocking context, we can create a runtime and use it to create the `blocking::Operator`.
95///
96/// > The following code uses a global statically created runtime as an example, please manage the
97/// > runtime on demand.
98///
99/// ```rust,no_run
100/// # use std::sync::LazyLock;
101/// # use opendal_core::services;
102/// # use opendal_core::blocking;
103/// # use opendal_core::Operator;
104/// # use opendal_core::Result;
105///
106/// static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| {
107/// tokio::runtime::Builder::new_multi_thread()
108/// .enable_all()
109/// .build()
110/// .unwrap()
111/// });
112///
113/// fn main() -> Result<()> {
114/// // Create fs backend builder.
115/// let builder = services::Memory::default();
116/// let op = Operator::new(builder)?.finish();
117///
118/// // Fetch the `EnterGuard` from global runtime.
119/// let _guard = RUNTIME.enter();
120/// // Build an `blocking::Operator` with blocking layer to start operating the storage.
121/// let _: blocking::Operator = blocking::Operator::new(op)?;
122///
123/// Ok(())
124/// }
125/// ```
126#[derive(Clone, Debug)]
127pub struct Operator {
128 handle: tokio::runtime::Handle,
129 op: AsyncOperator,
130}
131
132impl Operator {
133 /// Create a new `BlockingLayer` with the current runtime's handle
134 pub fn new(op: AsyncOperator) -> Result<Self> {
135 Ok(Self {
136 handle: Handle::try_current()
137 .map_err(|_| Error::new(ErrorKind::Unexpected, "failed to get current handle"))?,
138 op,
139 })
140 }
141
142 /// Spawn a future onto the runtime's worker pool and block until it
143 /// completes.
144 ///
145 /// Unlike [`Handle::block_on`] which polls the future on the **calling**
146 /// thread's stack, this method runs the future on a tokio worker thread
147 /// (typically 8 MB stack) and only uses the calling thread to wait for
148 /// the result. This avoids stack overflows when the async state machine
149 /// is deeply nested (e.g. HF/XET uploads driven from a JVM thread with
150 /// a 1 MB default stack).
151 fn spawn_block<F>(&self, f: F) -> Result<F::Output>
152 where
153 F: std::future::Future + Send + 'static,
154 F::Output: Send + 'static,
155 {
156 self.handle.block_on(self.handle.spawn(f)).map_err(|err| {
157 Error::new(ErrorKind::Unexpected, "blocking task failed").set_source(err)
158 })
159 }
160
161 /// Create a blocking operator from URI based configuration.
162 pub fn from_uri(uri: impl IntoOperatorUri) -> Result<Self> {
163 let op = AsyncOperator::from_uri(uri)?;
164 Self::new(op)
165 }
166
167 /// Get information of underlying accessor.
168 ///
169 /// # Examples
170 ///
171 /// ```
172 /// # use std::sync::Arc;
173 /// use opendal_core::blocking;
174 /// # use anyhow::Result;
175 /// use opendal_core::blocking::Operator;
176 ///
177 /// # fn test(op: blocking::Operator) -> Result<()> {
178 /// let info = op.info();
179 /// # Ok(())
180 /// # }
181 /// ```
182 pub fn info(&self) -> OperatorInfo {
183 self.op.info()
184 }
185}
186
187/// # Operator blocking API.
188impl Operator {
189 /// Create a presigned request for stat.
190 ///
191 /// See [`Operator::presign_stat`] for more details.
192 pub fn presign_stat(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
193 self.handle.block_on(self.op.presign_stat(path, expire))
194 }
195
196 /// Create a presigned request for read.
197 ///
198 /// See [`Operator::presign_read`] for more details.
199 pub fn presign_read(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
200 self.handle.block_on(self.op.presign_read(path, expire))
201 }
202
203 /// Create a presigned request for write.
204 ///
205 /// See [`Operator::presign_write`] for more details.
206 pub fn presign_write(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
207 self.handle.block_on(self.op.presign_write(path, expire))
208 }
209
210 /// Create a presigned request for delete.
211 ///
212 /// See [`Operator::presign_delete`] for more details.
213 pub fn presign_delete(&self, path: &str, expire: Duration) -> Result<PresignedRequest> {
214 self.handle.block_on(self.op.presign_delete(path, expire))
215 }
216
217 /// Get given path's metadata.
218 ///
219 /// # Behavior
220 ///
221 /// ## Services that support `create_dir`
222 ///
223 /// `test` and `test/` may vary in some services such as S3. However, on a local file system,
224 /// they're identical. Therefore, the behavior of `stat("test")` and `stat("test/")` might differ
225 /// in certain edge cases. Always use `stat("test/")` when you need to access a directory if possible.
226 ///
227 /// Here are the behavior list:
228 ///
229 /// | Case | Path | Result |
230 /// |------------------------|-----------------|--------------------------------------------|
231 /// | stat existing dir | `abc/` | Metadata with dir mode |
232 /// | stat existing file | `abc/def_file` | Metadata with file mode |
233 /// | stat dir without `/` | `abc/def_dir` | Error `NotFound` or metadata with dir mode |
234 /// | stat file with `/` | `abc/def_file/` | Error `NotFound` |
235 /// | stat not existing path | `xyz` | Error `NotFound` |
236 ///
237 /// Refer to [RFC: List Prefix][crate::docs::rfcs::rfc_3243_list_prefix] for more details.
238 ///
239 /// ## Services that not support `create_dir`
240 ///
241 /// For services that not support `create_dir`, `stat("test/")` will return `NotFound` even
242 /// when `test/abc` exists since the service won't have the concept of dir. There is nothing
243 /// we can do about this.
244 ///
245 /// # Examples
246 ///
247 /// ## Check if file exists
248 ///
249 /// ```
250 /// # use anyhow::Result;
251 /// # use futures::io;
252 /// use opendal_core::blocking;
253 /// # use opendal_core::blocking::Operator;
254 /// use opendal_core::ErrorKind;
255 /// #
256 /// # fn test(op: blocking::Operator) -> Result<()> {
257 /// if let Err(e) = op.stat("test") {
258 /// if e.kind() == ErrorKind::NotFound {
259 /// println!("file not exist")
260 /// }
261 /// }
262 /// # Ok(())
263 /// # }
264 /// ```
265 pub fn stat(&self, path: &str) -> Result<Metadata> {
266 self.stat_options(path, options::StatOptions::default())
267 }
268
269 /// Get given path's metadata with extra options.
270 ///
271 /// # Behavior
272 ///
273 /// ## Services that support `create_dir`
274 ///
275 /// `test` and `test/` may vary in some services such as S3. However, on a local file system,
276 /// they're identical. Therefore, the behavior of `stat("test")` and `stat("test/")` might differ
277 /// in certain edge cases. Always use `stat("test/")` when you need to access a directory if possible.
278 ///
279 /// Here are the behavior list:
280 ///
281 /// | Case | Path | Result |
282 /// |------------------------|-----------------|--------------------------------------------|
283 /// | stat existing dir | `abc/` | Metadata with dir mode |
284 /// | stat existing file | `abc/def_file` | Metadata with file mode |
285 /// | stat dir without `/` | `abc/def_dir` | Error `NotFound` or metadata with dir mode |
286 /// | stat file with `/` | `abc/def_file/` | Error `NotFound` |
287 /// | stat not existing path | `xyz` | Error `NotFound` |
288 ///
289 /// Refer to [RFC: List Prefix][crate::docs::rfcs::rfc_3243_list_prefix] for more details.
290 ///
291 /// ## Services that not support `create_dir`
292 ///
293 /// For services that not support `create_dir`, `stat("test/")` will return `NotFound` even
294 /// when `test/abc` exists since the service won't have the concept of dir. There is nothing
295 /// we can do about this.
296 pub fn stat_options(&self, path: &str, opts: options::StatOptions) -> Result<Metadata> {
297 let op = self.op.clone();
298 let path = path.to_string();
299 self.spawn_block(async move { op.stat_options(&path, opts).await })?
300 }
301
302 /// Check if this path exists or not.
303 ///
304 /// # Example
305 ///
306 /// ```no_run
307 /// use anyhow::Result;
308 /// use opendal_core::blocking;
309 /// use opendal_core::blocking::Operator;
310 /// fn test(op: blocking::Operator) -> Result<()> {
311 /// let _ = op.exists("test")?;
312 ///
313 /// Ok(())
314 /// }
315 /// ```
316 pub fn exists(&self, path: &str) -> Result<bool> {
317 let r = self.stat(path);
318 match r {
319 Ok(_) => Ok(true),
320 Err(err) => match err.kind() {
321 ErrorKind::NotFound => Ok(false),
322 _ => Err(err),
323 },
324 }
325 }
326
327 /// Create a dir at given path.
328 ///
329 /// # Notes
330 ///
331 /// To indicate that a path is a directory, it is compulsory to include
332 /// a trailing / in the path. Failure to do so may result in
333 /// `NotADirectory` error being returned by OpenDAL.
334 ///
335 /// # Behavior
336 ///
337 /// - Create on existing dir will succeed.
338 /// - Create dir is always recursive, works like `mkdir -p`
339 ///
340 /// # Examples
341 ///
342 /// ```no_run
343 /// # use opendal_core::Result;
344 /// use opendal_core::blocking;
345 /// # use opendal_core::blocking::Operator;
346 /// # use futures::TryStreamExt;
347 /// # fn test(op: blocking::Operator) -> Result<()> {
348 /// op.create_dir("path/to/dir/")?;
349 /// # Ok(())
350 /// # }
351 /// ```
352 pub fn create_dir(&self, path: &str) -> Result<()> {
353 let op = self.op.clone();
354 let path = path.to_string();
355 self.spawn_block(async move { op.create_dir(&path).await })?
356 }
357
358 /// Read the whole path into a bytes.
359 ///
360 /// This function will allocate a new bytes internally. For more precise memory control or
361 /// reading data lazily, please use [`blocking::Operator::reader`]
362 ///
363 /// # Examples
364 ///
365 /// ```no_run
366 /// # use opendal_core::Result;
367 /// use opendal_core::blocking;
368 /// # use opendal_core::blocking::Operator;
369 /// #
370 /// # fn test(op: blocking::Operator) -> Result<()> {
371 /// let bs = op.read("path/to/file")?;
372 /// # Ok(())
373 /// # }
374 /// ```
375 pub fn read(&self, path: &str) -> Result<Buffer> {
376 self.read_options(path, options::ReadOptions::default())
377 }
378
379 /// Read the whole path into a bytes with extra options.
380 ///
381 /// This function will allocate a new bytes internally. For more precise memory control or
382 /// reading data lazily, please use [`blocking::Operator::reader`]
383 pub fn read_options(&self, path: &str, opts: options::ReadOptions) -> Result<Buffer> {
384 let op = self.op.clone();
385 let path = path.to_string();
386 self.spawn_block(async move { op.read_options(&path, opts).await })?
387 }
388
389 /// Create a new reader which can read the whole path.
390 ///
391 /// # Examples
392 ///
393 /// ```no_run
394 /// # use opendal_core::Result;
395 /// use opendal_core::blocking;
396 /// # use opendal_core::blocking::Operator;
397 /// # use futures::TryStreamExt;
398 /// # fn test(op: blocking::Operator) -> Result<()> {
399 /// let r = op.reader("path/to/file")?;
400 /// # Ok(())
401 /// # }
402 /// ```
403 pub fn reader(&self, path: &str) -> Result<blocking::Reader> {
404 self.reader_options(path, options::ReaderOptions::default())
405 }
406
407 /// Create a new reader with extra options
408 pub fn reader_options(
409 &self,
410 path: &str,
411 opts: options::ReaderOptions,
412 ) -> Result<blocking::Reader> {
413 let r = self.handle.block_on(self.op.reader_options(path, opts))?;
414 Ok(blocking::Reader::new(self.handle.clone(), r))
415 }
416
417 /// Write bytes into given path.
418 ///
419 /// # Notes
420 ///
421 /// - Write will make sure all bytes has been written, or an error will be returned.
422 ///
423 /// # Examples
424 ///
425 /// ```no_run
426 /// # use opendal_core::Result;
427 /// # use opendal_core::blocking::Operator;
428 /// # use futures::StreamExt;
429 /// # use futures::SinkExt;
430 /// use bytes::Bytes;
431 /// use opendal_core::blocking;
432 ///
433 /// # fn test(op: blocking::Operator) -> Result<()> {
434 /// op.write("path/to/file", vec![0; 4096])?;
435 /// # Ok(())
436 /// # }
437 /// ```
438 pub fn write(&self, path: &str, bs: impl Into<Buffer>) -> Result<Metadata> {
439 self.write_options(path, bs, options::WriteOptions::default())
440 }
441
442 /// Write data with options.
443 ///
444 /// # Notes
445 ///
446 /// - Write will make sure all bytes has been written, or an error will be returned.
447 pub fn write_options(
448 &self,
449 path: &str,
450 bs: impl Into<Buffer>,
451 opts: options::WriteOptions,
452 ) -> Result<Metadata> {
453 let op = self.op.clone();
454 let path = path.to_string();
455 let bs = bs.into();
456 self.spawn_block(async move { op.write_options(&path, bs, opts).await })?
457 }
458
459 /// Write multiple bytes into given path.
460 ///
461 /// # Notes
462 ///
463 /// - Write will make sure all bytes has been written, or an error will be returned.
464 ///
465 /// # Examples
466 ///
467 /// ```no_run
468 /// # use opendal_core::Result;
469 /// # use opendal_core::blocking;
470 /// # use opendal_core::blocking::Operator;
471 /// # use futures::StreamExt;
472 /// # use futures::SinkExt;
473 /// use bytes::Bytes;
474 ///
475 /// # fn test(op: blocking::Operator) -> Result<()> {
476 /// let mut w = op.writer("path/to/file")?;
477 /// w.write(vec![0; 4096])?;
478 /// w.write(vec![1; 4096])?;
479 /// w.close()?;
480 /// # Ok(())
481 /// # }
482 /// ```
483 pub fn writer(&self, path: &str) -> Result<blocking::Writer> {
484 self.writer_options(path, options::WriteOptions::default())
485 }
486
487 /// Create a new writer with extra options
488 pub fn writer_options(
489 &self,
490 path: &str,
491 opts: options::WriteOptions,
492 ) -> Result<blocking::Writer> {
493 let w = self.handle.block_on(self.op.writer_options(path, opts))?;
494 Ok(blocking::Writer::new(self.handle.clone(), w))
495 }
496
497 /// Copy a file from `from` to `to`.
498 ///
499 /// # Notes
500 ///
501 /// - `from` and `to` must be a file.
502 /// - `to` will be overwritten if it exists.
503 /// - If `from` and `to` are the same, nothing will happen.
504 /// - `copy` is idempotent. For same `from` and `to` input, the result will be the same.
505 ///
506 /// # Examples
507 ///
508 /// ```
509 /// # use opendal_core::Result;
510 /// use opendal_core::blocking;
511 /// # use opendal_core::blocking::Operator;
512 ///
513 /// # fn test(op: blocking::Operator) -> Result<()> {
514 /// op.copy("path/to/file", "path/to/file2")?;
515 /// # Ok(())
516 /// # }
517 /// ```
518 pub fn copy(&self, from: &str, to: &str) -> Result<()> {
519 let op = self.op.clone();
520 let from = from.to_string();
521 let to = to.to_string();
522 self.spawn_block(async move { op.copy(&from, &to).await })?
523 }
524
525 /// Rename a file from `from` to `to`.
526 ///
527 /// # Notes
528 ///
529 /// - `from` and `to` must be a file.
530 /// - `to` will be overwritten if it exists.
531 /// - If `from` and `to` are the same, a `IsSameFile` error will occur.
532 ///
533 /// # Examples
534 ///
535 /// ```
536 /// # use opendal_core::Result;
537 /// use opendal_core::blocking;
538 /// # use opendal_core::blocking::Operator;
539 ///
540 /// # fn test(op: blocking::Operator) -> Result<()> {
541 /// op.rename("path/to/file", "path/to/file2")?;
542 /// # Ok(())
543 /// # }
544 /// ```
545 pub fn rename(&self, from: &str, to: &str) -> Result<()> {
546 let op = self.op.clone();
547 let from = from.to_string();
548 let to = to.to_string();
549 self.spawn_block(async move { op.rename(&from, &to).await })?
550 }
551
552 /// Delete given path.
553 ///
554 /// # Notes
555 ///
556 /// - Delete not existing error won't return errors.
557 ///
558 /// # Examples
559 ///
560 /// ```no_run
561 /// # use anyhow::Result;
562 /// # use futures::io;
563 /// use opendal_core::blocking;
564 /// # use opendal_core::blocking::Operator;
565 /// # fn test(op: blocking::Operator) -> Result<()> {
566 /// op.delete("path/to/file")?;
567 /// # Ok(())
568 /// # }
569 /// ```
570 pub fn delete(&self, path: &str) -> Result<()> {
571 self.delete_options(path, options::DeleteOptions::default())
572 }
573
574 /// Delete given path with options.
575 ///
576 /// # Notes
577 ///
578 /// - Delete not existing error won't return errors.
579 pub fn delete_options(&self, path: &str, opts: options::DeleteOptions) -> Result<()> {
580 let op = self.op.clone();
581 let path = path.to_string();
582 self.spawn_block(async move { op.delete_options(&path, opts).await })?
583 }
584
585 /// Delete an infallible iterator of paths.
586 ///
587 /// Also see:
588 ///
589 /// - [`blocking::Operator::delete_try_iter`]: delete an fallible iterator of paths.
590 pub fn delete_iter<I, D>(&self, iter: I) -> Result<()>
591 where
592 I: IntoIterator<Item = D>,
593 D: IntoDeleteInput,
594 {
595 self.handle.block_on(self.op.delete_iter(iter))
596 }
597
598 /// Delete a fallible iterator of paths.
599 ///
600 /// Also see:
601 ///
602 /// - [`blocking::Operator::delete_iter`]: delete an infallible iterator of paths.
603 pub fn delete_try_iter<I, D>(&self, try_iter: I) -> Result<()>
604 where
605 I: IntoIterator<Item = Result<D>>,
606 D: IntoDeleteInput,
607 {
608 self.handle.block_on(self.op.delete_try_iter(try_iter))
609 }
610
611 /// Create a [`BlockingDeleter`] to continuously remove content from storage.
612 ///
613 /// It leverages batch deletion capabilities provided by storage services for efficient removal.
614 ///
615 /// Users can have more control over the deletion process by using [`BlockingDeleter`] directly.
616 pub fn deleter(&self) -> Result<blocking::Deleter> {
617 blocking::Deleter::create(
618 self.handle.clone(),
619 self.handle.block_on(self.op.deleter())?,
620 )
621 }
622
623 /// Remove the path and all nested dirs and files recursively.
624 ///
625 /// # Deprecated
626 ///
627 /// This method is deprecated since v0.55.0. Use [`blocking::Operator::delete_options`] with
628 /// `recursive: true` instead.
629 ///
630 /// ## Migration Example
631 ///
632 /// Instead of:
633 /// ```ignore
634 /// op.remove_all("path/to/dir")?;
635 /// ```
636 ///
637 /// Use:
638 /// ```ignore
639 /// use opendal_core::options::DeleteOptions;
640 /// op.delete_options("path/to/dir", DeleteOptions {
641 /// recursive: true,
642 /// ..Default::default()
643 /// })?;
644 /// ```
645 ///
646 /// # Notes
647 ///
648 /// If underlying services support delete in batch, we will use batch
649 /// delete instead.
650 ///
651 /// # Examples
652 ///
653 /// ```
654 /// # use anyhow::Result;
655 /// # use futures::io;
656 /// use opendal_core::blocking;
657 /// # use opendal_core::blocking::Operator;
658 /// # fn test(op: blocking::Operator) -> Result<()> {
659 /// op.remove_all("path/to/dir")?;
660 /// # Ok(())
661 /// # }
662 /// ```
663 #[deprecated(
664 since = "0.55.0",
665 note = "Use `delete_options` with `recursive: true` instead"
666 )]
667 #[allow(deprecated)]
668 pub fn remove_all(&self, path: &str) -> Result<()> {
669 self.delete_options(
670 path,
671 options::DeleteOptions {
672 recursive: true,
673 ..Default::default()
674 },
675 )
676 }
677
678 /// List entries whose paths start with the given prefix `path`.
679 ///
680 /// # Semantics
681 ///
682 /// - Listing is **prefix-based**; it doesn't require the parent directory to exist.
683 /// - If `path` itself exists, it is returned as an entry along with prefixed children.
684 /// - If `path` is missing but deeper objects exist, the list succeeds and returns those prefixed entries instead of an error.
685 /// - Set `recursive` in [`options::ListOptions`] via [`list_options`](Self::list_options) to walk all descendants; the default returns only immediate children when delimiter is supported.
686 ///
687 /// ## Streaming List
688 ///
689 /// This function materializes the full result in memory. For large listings, prefer [`blocking::Operator::lister`] to stream entries.
690 ///
691 /// # Examples
692 ///
693 /// ```no_run
694 /// # use anyhow::Result;
695 /// use opendal_core::blocking;
696 /// use opendal_core::blocking::Operator;
697 /// use opendal_core::EntryMode;
698 /// # fn test(op: blocking::Operator) -> Result<()> {
699 /// let mut entries = op.list("path/to/dir/")?;
700 /// for entry in entries {
701 /// match entry.metadata().mode() {
702 /// EntryMode::FILE => {
703 /// println!("Handling file")
704 /// }
705 /// EntryMode::DIR => {
706 /// println!("Handling dir {}", entry.path())
707 /// }
708 /// EntryMode::Unknown => continue,
709 /// }
710 /// }
711 /// # Ok(())
712 /// # }
713 /// ```
714 pub fn list(&self, path: &str) -> Result<Vec<Entry>> {
715 self.list_options(path, options::ListOptions::default())
716 }
717
718 /// List entries whose paths start with the given prefix `path` with additional options.
719 ///
720 /// # Semantics
721 ///
722 /// Inherits the prefix semantics described in [`Operator::list`] (blocking variant). It returns `path` itself if present and tolerates missing parents when prefixed objects exist.
723 ///
724 /// ## Streaming List
725 ///
726 /// This function materializes the full result in memory. For large listings, prefer [`blocking::Operator::lister`] to stream entries.
727 ///
728 /// ## Options
729 ///
730 /// See [`options::ListOptions`] for the full set. Common knobs: traversal (`recursive`), pagination (`limit`, `start_after`), and versioning (`versions`, `deleted`).
731 pub fn list_options(&self, path: &str, opts: options::ListOptions) -> Result<Vec<Entry>> {
732 let op = self.op.clone();
733 let path = path.to_string();
734 self.spawn_block(async move { op.list_options(&path, opts).await })?
735 }
736
737 /// Create a streaming lister for entries whose paths start with the given prefix `path`.
738 ///
739 /// This function creates a new [`BlockingLister`]; dropping it stops listing.
740 ///
741 /// # Semantics
742 ///
743 /// Shares the same prefix semantics as [`blocking::Operator::list`]: parent directory is optional; `path` itself is yielded if present; missing parents with deeper objects are accepted.
744 ///
745 /// ## Options
746 ///
747 /// Accepts the same [`options::ListOptions`] as [`list_options`](Self::list_options): traversal (`recursive`), pagination (`limit`, `start_after`), and versioning (`versions`, `deleted`).
748 ///
749 /// # Examples
750 ///
751 /// ```no_run
752 /// # use anyhow::Result;
753 /// # use futures::io;
754 /// use futures::TryStreamExt;
755 /// use opendal_core::blocking;
756 /// use opendal_core::blocking::Operator;
757 /// use opendal_core::EntryMode;
758 /// # fn test(op: blocking::Operator) -> Result<()> {
759 /// let mut ds = op.lister("path/to/dir/")?;
760 /// for de in ds {
761 /// let de = de?;
762 /// match de.metadata().mode() {
763 /// EntryMode::FILE => {
764 /// println!("Handling file")
765 /// }
766 /// EntryMode::DIR => {
767 /// println!("Handling dir like start a new list via meta.path()")
768 /// }
769 /// EntryMode::Unknown => continue,
770 /// }
771 /// }
772 /// # Ok(())
773 /// # }
774 /// ```
775 pub fn lister(&self, path: &str) -> Result<blocking::Lister> {
776 self.lister_options(path, options::ListOptions::default())
777 }
778
779 /// List entries under a prefix as an iterator with options.
780 ///
781 /// This function creates a new handle to stream entries and inherits the prefix semantics of [`blocking::Operator::list`].
782 ///
783 /// ## Options
784 ///
785 /// Same as [`lister`](Self::lister); see [`options::ListOptions`] for traversal, pagination, and versioning knobs.
786 pub fn lister_options(
787 &self,
788 path: &str,
789 opts: options::ListOptions,
790 ) -> Result<blocking::Lister> {
791 let l = self.handle.block_on(self.op.lister_options(path, opts))?;
792 Ok(blocking::Lister::new(self.handle.clone(), l))
793 }
794
795 /// Check if this operator can work correctly.
796 ///
797 /// We will send a `list` request to path and return any errors we met.
798 ///
799 /// ```
800 /// # use std::sync::Arc;
801 /// # use anyhow::Result;
802 /// use opendal_core::blocking;
803 /// use opendal_core::blocking::Operator;
804 /// use opendal_core::ErrorKind;
805 ///
806 /// # fn test(op: blocking::Operator) -> Result<()> {
807 /// op.check()?;
808 /// # Ok(())
809 /// # }
810 /// ```
811 pub fn check(&self) -> Result<()> {
812 let mut ds = self.lister("/")?;
813
814 match ds.next() {
815 Some(Err(e)) if e.kind() != ErrorKind::NotFound => Err(e),
816 _ => Ok(()),
817 }
818 }
819}
820
821impl From<Operator> for AsyncOperator {
822 fn from(val: Operator) -> Self {
823 val.op
824 }
825}