Skip to main content

gproxy_file/
lib.rs

1//! Optional file-content storage using OpenDAL's filesystem and S3 services.
2//!
3//! The common API is [`Operator`]: read/write, streaming readers/writers, stat,
4//! list and delete. Backend behavior and errors come from OpenDAL. Application
5//! entities, ownership, retention and resource-ID mappings stay with the caller.
6
7pub use opendal_core as opendal;
8pub use opendal_core::{Buffer, Error, ErrorKind, Metadata, Operator, Result};
9
10#[cfg(all(feature = "fs", not(target_arch = "wasm32")))]
11pub use opendal_service_fs::{Fs, FsConfig};
12#[cfg(feature = "s3")]
13pub use opendal_service_s3::{S3, S3Config};
14
15/// Open a local filesystem root using OpenDAL's filesystem service.
16#[cfg(all(feature = "fs", not(target_arch = "wasm32")))]
17pub fn filesystem(root: &str) -> Result<Operator> {
18    Operator::new(Fs::default().root(root))
19}
20
21/// Open S3-compatible storage with the default reqwest transport.
22/// Native targets use rustls; WASM uses the host Fetch API.
23#[cfg(feature = "s3")]
24pub fn s3(builder: S3) -> Result<Operator> {
25    s3_with_transport(
26        builder,
27        opendal::HttpTransporter::new(opendal_http_transport_reqwest::ReqwestTransport::default()),
28    )
29}
30
31/// Open S3-compatible storage with a caller-supplied HTTP transport.
32/// This lets a host reuse its client configuration without a global override.
33#[cfg(feature = "s3")]
34pub fn s3_with_transport(builder: S3, transport: opendal::HttpTransporter) -> Result<Operator> {
35    let context = opendal::OperationContext::new().with_http_transport(transport);
36    #[cfg(target_arch = "wasm32")]
37    let context = context.with_executor(opendal::Executor::with(WasmExecutor));
38    Ok(Operator::new(builder)?.with_context(context))
39}
40
41// OpenDAL needs an executor for concurrent multipart work. Workers do not run
42// Tokio; use the WASM microtask executor for the same OpenDAL task lifecycle.
43#[cfg(all(feature = "s3", target_arch = "wasm32"))]
44#[derive(Debug)]
45struct WasmExecutor;
46
47#[cfg(all(feature = "s3", target_arch = "wasm32"))]
48impl opendal::Execute for WasmExecutor {
49    fn execute(&self, task: opendal::raw::BoxedStaticFuture<()>) {
50        wasm_bindgen_futures::spawn_local(task);
51    }
52}