Skip to main content

opendal/
lib.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
18#![doc(html_logo_url = "https://opendal.apache.org/img/logo.svg")]
19#![doc = include_str!("../README.md")]
20#![cfg_attr(docsrs, feature(doc_cfg))]
21#![cfg_attr(docsrs, doc(auto_cfg))]
22#![deny(missing_docs)]
23
24pub use opendal_core::*;
25
26/// Runtime executor implementations.
27pub mod executors {
28    #[cfg(all(feature = "executors-compio", not(target_arch = "wasm32")))]
29    pub use opendal_executor_compio::CompioExecutor;
30    #[cfg(feature = "executors-tokio")]
31    pub use opendal_executor_tokio::TokioExecutor;
32}
33
34#[cfg(any(
35    feature = "http-transport-cyper-native-tls",
36    feature = "http-transport-cyper-rustls"
37))]
38pub use opendal_http_transport_cyper::CyperTransport;
39
40#[cfg(feature = "tests")]
41pub extern crate opendal_testkit as tests;
42
43/// Install the global defaults provided by the facade crate.
44///
45/// This function is safe to call multiple times. It registers enabled services
46/// and installs the default executor and HTTP transport when their features are
47/// enabled.
48///
49/// # Process-wide defaults
50///
51/// When `auto-register-services` is enabled, a process constructor calls this
52/// function before `main`. The first installed HTTP transport and executor
53/// remain the process-wide defaults.
54///
55/// Applications that need to install their own process-wide transport should
56/// disable `auto-register-services`, call [`init_default_registry`] explicitly
57/// when URI-based construction is needed, and install the transport themselves.
58/// If `auto-register-services` is disabled, call this function to install the
59/// facade defaults, or configure your own defaults with
60/// [`HttpTransporter::install_default`] and [`Executor::install_default`].
61/// Use [`OperationContext`] to configure runtime resources for one operator.
62pub fn install_default() {
63    init_default_registry();
64
65    #[cfg(feature = "executors-tokio")]
66    opendal_executor_tokio::install_default();
67
68    #[cfg(any(
69        feature = "http-transport-reqwest",
70        feature = "http-transport-reqwest-native-tls",
71        feature = "http-transport-reqwest-rustls",
72        feature = "http-transport-reqwest-rustls-no-provider"
73    ))]
74    opendal_http_transport_reqwest::install_default();
75}
76
77#[cfg(all(
78    test,
79    feature = "auto-register-services",
80    feature = "executors-tokio",
81    not(target_arch = "wasm32")
82))]
83mod default_executor_tests {
84    #[tokio::test]
85    async fn default_executor_runs_concurrent_reads() -> crate::Result<()> {
86        let op = crate::Operator::via_iter("memory", [])?;
87        op.write("file", "abcdefgh").await?;
88        let content = op
89            .read_with("file")
90            .range(0..8)
91            .chunk(2)
92            .concurrent(2)
93            .await?;
94        assert_eq!(content.to_bytes(), "abcdefgh");
95        Ok(())
96    }
97}
98
99/// Initialize the global [`OperatorRegistry`] with enabled services.
100///
101/// This function is safe to call multiple times and will only perform
102/// initialization once.
103///
104/// # Notes
105///
106/// Some bindings link `opendal` as a `staticlib`, where `#[ctor::ctor]`
107/// initializers may not be executed due to linker behavior. Those bindings
108/// should call [`install_default`] explicitly before using `Operator::from_uri`
109/// or `Operator::via_iter`.
110pub fn init_default_registry() {
111    static DEFAULT_REGISTRY_INIT: std::sync::Once = std::sync::Once::new();
112    DEFAULT_REGISTRY_INIT.call_once(|| init_default_registry_inner(OperatorRegistry::get()));
113}
114
115fn init_default_registry_inner(registry: &OperatorRegistry) {
116    opendal_core::services::register_memory_service(registry);
117
118    #[cfg(feature = "services-aliyun-drive")]
119    opendal_service_aliyun_drive::register_aliyun_drive_service(registry);
120
121    #[cfg(feature = "services-alluxio")]
122    opendal_service_alluxio::register_alluxio_service(registry);
123
124    #[cfg(feature = "services-azblob")]
125    opendal_service_azblob::register_azblob_service(registry);
126
127    #[cfg(feature = "services-azdls")]
128    opendal_service_azdls::register_azdls_service(registry);
129
130    #[cfg(feature = "services-azfile")]
131    opendal_service_azfile::register_azfile_service(registry);
132
133    #[cfg(feature = "services-b2")]
134    opendal_service_b2::register_b2_service(registry);
135
136    #[cfg(feature = "services-cacache")]
137    opendal_service_cacache::register_cacache_service(registry);
138
139    #[cfg(feature = "services-cloudflare-kv")]
140    opendal_service_cloudflare_kv::register_cloudflare_kv_service(registry);
141
142    #[cfg(feature = "services-compfs")]
143    opendal_service_compfs::register_compfs_service(registry);
144
145    #[cfg(feature = "services-cos")]
146    opendal_service_cos::register_cos_service(registry);
147
148    #[cfg(feature = "services-d1")]
149    opendal_service_d1::register_d1_service(registry);
150
151    #[cfg(feature = "services-dashmap")]
152    opendal_service_dashmap::register_dashmap_service(registry);
153
154    #[cfg(feature = "services-dbfs")]
155    opendal_service_dbfs::register_dbfs_service(registry);
156
157    #[cfg(feature = "services-dropbox")]
158    opendal_service_dropbox::register_dropbox_service(registry);
159
160    #[cfg(feature = "services-etcd")]
161    opendal_service_etcd::register_etcd_service(registry);
162
163    #[cfg(feature = "services-foundationdb")]
164    opendal_service_foundationdb::register_foundationdb_service(registry);
165
166    #[cfg(feature = "services-foyer")]
167    opendal_service_foyer::register_foyer_service(registry);
168
169    #[cfg(feature = "services-fs")]
170    opendal_service_fs::register_fs_service(registry);
171
172    #[cfg(feature = "services-ftp")]
173    opendal_service_ftp::register_ftp_service(registry);
174
175    #[cfg(feature = "services-gcs")]
176    opendal_service_gcs::register_gcs_service(registry);
177
178    #[cfg(feature = "services-gcs-grpc")]
179    opendal_service_gcs_grpc::register_gcs_grpc_service(registry);
180
181    #[cfg(feature = "services-gdrive")]
182    opendal_service_gdrive::register_gdrive_service(registry);
183
184    #[cfg(feature = "services-ghac")]
185    opendal_service_ghac::register_ghac_service(registry);
186
187    #[cfg(feature = "services-git")]
188    opendal_service_git::register_git_service(registry);
189
190    #[cfg(feature = "services-github")]
191    opendal_service_github::register_github_service(registry);
192
193    #[cfg(feature = "services-goosefs")]
194    opendal_service_goosefs::register_goosefs_service(registry);
195
196    #[cfg(feature = "services-gridfs")]
197    opendal_service_gridfs::register_gridfs_service(registry);
198
199    #[cfg(feature = "services-hdfs")]
200    opendal_service_hdfs::register_hdfs_service(registry);
201
202    #[cfg(feature = "services-hdfs-native")]
203    opendal_service_hdfs_native::register_hdfs_native_service(registry);
204
205    #[cfg(feature = "services-http")]
206    opendal_service_http::register_http_service(registry);
207
208    #[cfg(feature = "services-hf")]
209    opendal_service_hf::register_hf_service(registry);
210
211    #[cfg(all(feature = "services-hf3fs", target_os = "linux"))]
212    opendal_service_hf3fs::register_hf3fs_service(registry);
213
214    #[cfg(feature = "services-ipfs")]
215    opendal_service_ipfs::register_ipfs_service(registry);
216
217    #[cfg(feature = "services-ipmfs")]
218    opendal_service_ipmfs::register_ipmfs_service(registry);
219
220    #[cfg(feature = "services-koofr")]
221    opendal_service_koofr::register_koofr_service(registry);
222
223    #[cfg(feature = "services-lakefs")]
224    opendal_service_lakefs::register_lakefs_service(registry);
225
226    #[cfg(feature = "services-memcached")]
227    opendal_service_memcached::register_memcached_service(registry);
228
229    #[cfg(feature = "services-mini-moka")]
230    opendal_service_mini_moka::register_mini_moka_service(registry);
231
232    #[cfg(feature = "services-moka")]
233    opendal_service_moka::register_moka_service(registry);
234
235    #[cfg(feature = "services-mongodb")]
236    opendal_service_mongodb::register_mongodb_service(registry);
237
238    #[cfg(feature = "services-monoiofs")]
239    opendal_service_monoiofs::register_monoiofs_service(registry);
240
241    #[cfg(feature = "services-mysql")]
242    opendal_service_mysql::register_mysql_service(registry);
243
244    #[cfg(feature = "services-obs")]
245    opendal_service_obs::register_obs_service(registry);
246
247    #[cfg(feature = "services-onedrive")]
248    opendal_service_onedrive::register_onedrive_service(registry);
249
250    #[cfg(feature = "services-oss")]
251    opendal_service_oss::register_oss_service(registry);
252
253    #[cfg(feature = "services-pcloud")]
254    opendal_service_pcloud::register_pcloud_service(registry);
255
256    #[cfg(feature = "services-persy")]
257    opendal_service_persy::register_persy_service(registry);
258
259    #[cfg(feature = "services-postgresql")]
260    opendal_service_postgresql::register_postgresql_service(registry);
261
262    #[cfg(feature = "services-redb")]
263    opendal_service_redb::register_redb_service(registry);
264
265    #[cfg(feature = "services-rocksdb")]
266    opendal_service_rocksdb::register_rocksdb_service(registry);
267
268    #[cfg(feature = "services-s3")]
269    opendal_service_s3::register_s3_service(registry);
270
271    #[cfg(feature = "services-r2")]
272    opendal_service_s3::register_r2_service(registry);
273
274    #[cfg(feature = "services-minio")]
275    opendal_service_s3::register_minio_service(registry);
276
277    #[cfg(feature = "services-seafile")]
278    opendal_service_seafile::register_seafile_service(registry);
279
280    #[cfg(feature = "services-sftp")]
281    opendal_service_sftp::register_sftp_service(registry);
282
283    #[cfg(feature = "services-sled")]
284    opendal_service_sled::register_sled_service(registry);
285
286    #[cfg(feature = "services-smb")]
287    opendal_service_smb::register_smb_service(registry);
288
289    #[cfg(feature = "services-sqlite")]
290    opendal_service_sqlite::register_sqlite_service(registry);
291
292    #[cfg(feature = "services-surrealdb")]
293    opendal_service_surrealdb::register_surrealdb_service(registry);
294
295    #[cfg(feature = "services-swift")]
296    opendal_service_swift::register_swift_service(registry);
297
298    #[cfg(feature = "services-tikv")]
299    opendal_service_tikv::register_tikv_service(registry);
300
301    #[cfg(feature = "services-tos")]
302    opendal_service_tos::register_tos_service(registry);
303
304    #[cfg(feature = "services-upyun")]
305    opendal_service_upyun::register_upyun_service(registry);
306
307    #[cfg(feature = "services-vercel-artifacts")]
308    opendal_service_vercel_artifacts::register_vercel_artifacts_service(registry);
309
310    #[cfg(feature = "services-vercel-blob")]
311    opendal_service_vercel_blob::register_vercel_blob_service(registry);
312
313    #[cfg(feature = "services-webdav")]
314    opendal_service_webdav::register_webdav_service(registry);
315
316    #[cfg(feature = "services-webhdfs")]
317    opendal_service_webhdfs::register_webhdfs_service(registry);
318
319    #[cfg(feature = "services-yandex-disk")]
320    opendal_service_yandex_disk::register_yandex_disk_service(registry);
321
322    #[cfg(all(target_arch = "wasm32", feature = "services-opfs"))]
323    opendal_service_opfs::register_opfs_service(registry);
324
325    #[cfg(any(feature = "services-redis", feature = "services-redis-native-tls"))]
326    opendal_service_redis::register_redis_service(registry);
327}
328
329#[cfg(feature = "auto-register-services")]
330#[ctor::ctor(unsafe)]
331fn register_default_operator_registry() {
332    install_default();
333}
334
335/// Service builders enabled through `services-*` Cargo features.
336///
337/// [`Memory`](services::Memory) is always available. Other builders are
338/// re-exported when their matching facade feature is enabled; for example,
339/// `services-r2` enables the `R2` builder.
340///
341/// Pass a configured builder to [`Operator::new`]. The facade automatically
342/// registers enabled services for [`Operator::from_uri`] and
343/// [`Operator::via_iter`] when the `auto-register-services` feature is enabled.
344pub mod services {
345    pub use opendal_core::services::*;
346    #[cfg(feature = "services-aliyun-drive")]
347    pub use opendal_service_aliyun_drive::*;
348    #[cfg(feature = "services-alluxio")]
349    pub use opendal_service_alluxio::*;
350    #[cfg(feature = "services-azblob")]
351    pub use opendal_service_azblob::*;
352    #[cfg(feature = "services-azdls")]
353    pub use opendal_service_azdls::*;
354    #[cfg(feature = "services-azfile")]
355    pub use opendal_service_azfile::*;
356    #[cfg(feature = "services-b2")]
357    pub use opendal_service_b2::*;
358    #[cfg(feature = "services-cacache")]
359    pub use opendal_service_cacache::*;
360    #[cfg(feature = "services-cloudflare-kv")]
361    pub use opendal_service_cloudflare_kv::*;
362    #[cfg(feature = "services-compfs")]
363    pub use opendal_service_compfs::*;
364    #[cfg(feature = "services-cos")]
365    pub use opendal_service_cos::*;
366    #[cfg(feature = "services-d1")]
367    pub use opendal_service_d1::*;
368    #[cfg(feature = "services-dashmap")]
369    pub use opendal_service_dashmap::*;
370    #[cfg(feature = "services-dbfs")]
371    pub use opendal_service_dbfs::*;
372    #[cfg(feature = "services-dropbox")]
373    pub use opendal_service_dropbox::*;
374    #[cfg(feature = "services-etcd")]
375    pub use opendal_service_etcd::*;
376    #[cfg(feature = "services-foundationdb")]
377    pub use opendal_service_foundationdb::*;
378    #[cfg(feature = "services-foyer")]
379    pub use opendal_service_foyer::*;
380    #[cfg(feature = "services-fs")]
381    pub use opendal_service_fs::*;
382    #[cfg(feature = "services-ftp")]
383    pub use opendal_service_ftp::*;
384    #[cfg(feature = "services-gcs")]
385    pub use opendal_service_gcs::*;
386    #[cfg(feature = "services-gcs-grpc")]
387    pub use opendal_service_gcs_grpc::*;
388    #[cfg(feature = "services-gdrive")]
389    pub use opendal_service_gdrive::*;
390    #[cfg(feature = "services-ghac")]
391    pub use opendal_service_ghac::*;
392    #[cfg(feature = "services-git")]
393    pub use opendal_service_git::*;
394    #[cfg(feature = "services-github")]
395    pub use opendal_service_github::*;
396    #[cfg(feature = "services-goosefs")]
397    pub use opendal_service_goosefs::*;
398    #[cfg(feature = "services-gridfs")]
399    pub use opendal_service_gridfs::*;
400    #[cfg(feature = "services-hdfs")]
401    pub use opendal_service_hdfs::*;
402    #[cfg(feature = "services-hdfs-native")]
403    pub use opendal_service_hdfs_native::*;
404    #[cfg(feature = "services-hf")]
405    pub use opendal_service_hf::*;
406    #[cfg(all(feature = "services-hf3fs", target_os = "linux"))]
407    pub use opendal_service_hf3fs::*;
408    #[cfg(feature = "services-http")]
409    pub use opendal_service_http::*;
410    #[cfg(feature = "services-ipfs")]
411    pub use opendal_service_ipfs::*;
412    #[cfg(feature = "services-ipmfs")]
413    pub use opendal_service_ipmfs::*;
414    #[cfg(feature = "services-koofr")]
415    pub use opendal_service_koofr::*;
416    #[cfg(feature = "services-lakefs")]
417    pub use opendal_service_lakefs::*;
418    #[cfg(feature = "services-memcached")]
419    pub use opendal_service_memcached::*;
420    #[cfg(feature = "services-mini-moka")]
421    pub use opendal_service_mini_moka::*;
422    #[cfg(feature = "services-moka")]
423    pub use opendal_service_moka::*;
424    #[cfg(feature = "services-mongodb")]
425    pub use opendal_service_mongodb::*;
426    #[cfg(feature = "services-monoiofs")]
427    pub use opendal_service_monoiofs::*;
428    #[cfg(feature = "services-mysql")]
429    pub use opendal_service_mysql::*;
430    #[cfg(feature = "services-obs")]
431    pub use opendal_service_obs::*;
432    #[cfg(feature = "services-onedrive")]
433    pub use opendal_service_onedrive::*;
434    #[cfg(all(target_arch = "wasm32", feature = "services-opfs"))]
435    pub use opendal_service_opfs::*;
436    #[cfg(feature = "services-oss")]
437    pub use opendal_service_oss::*;
438    #[cfg(feature = "services-pcloud")]
439    pub use opendal_service_pcloud::*;
440    #[cfg(feature = "services-persy")]
441    pub use opendal_service_persy::*;
442    #[cfg(feature = "services-postgresql")]
443    pub use opendal_service_postgresql::*;
444    #[cfg(feature = "services-redb")]
445    pub use opendal_service_redb::*;
446    #[cfg(feature = "services-redis")]
447    pub use opendal_service_redis::*;
448    #[cfg(feature = "services-rocksdb")]
449    pub use opendal_service_rocksdb::*;
450    #[cfg(feature = "services-minio")]
451    pub use opendal_service_s3::{MINIO_SCHEME, Minio, MinioConfig, register_minio_service};
452    #[cfg(feature = "services-r2")]
453    pub use opendal_service_s3::{R2, R2_SCHEME, R2Config, register_r2_service};
454    #[cfg(feature = "services-s3")]
455    pub use opendal_service_s3::{S3, S3_SCHEME, S3Config, register_s3_service, reqsign_aws_v4};
456    #[cfg(feature = "services-seafile")]
457    pub use opendal_service_seafile::*;
458    #[cfg(feature = "services-sftp")]
459    pub use opendal_service_sftp::*;
460    #[cfg(feature = "services-sled")]
461    pub use opendal_service_sled::*;
462    #[cfg(feature = "services-smb")]
463    pub use opendal_service_smb::*;
464    #[cfg(feature = "services-sqlite")]
465    pub use opendal_service_sqlite::*;
466    #[cfg(feature = "services-surrealdb")]
467    pub use opendal_service_surrealdb::*;
468    #[cfg(feature = "services-swift")]
469    pub use opendal_service_swift::*;
470    #[cfg(feature = "services-tikv")]
471    pub use opendal_service_tikv::*;
472    #[cfg(feature = "services-tos")]
473    pub use opendal_service_tos::*;
474    #[cfg(feature = "services-upyun")]
475    pub use opendal_service_upyun::*;
476    #[cfg(feature = "services-vercel-artifacts")]
477    pub use opendal_service_vercel_artifacts::*;
478    #[cfg(feature = "services-vercel-blob")]
479    pub use opendal_service_vercel_blob::*;
480    #[cfg(feature = "services-webdav")]
481    pub use opendal_service_webdav::*;
482    #[cfg(feature = "services-webhdfs")]
483    pub use opendal_service_webhdfs::*;
484    #[cfg(feature = "services-yandex-disk")]
485    pub use opendal_service_yandex_disk::*;
486}
487
488#[cfg(test)]
489mod service_feature_tests {
490    use super::*;
491
492    #[test]
493    fn registers_enabled_s3_provider_schemes() {
494        init_default_registry();
495        let schemes = OperatorRegistry::get().schemes();
496
497        assert!(schemes.contains("memory"));
498
499        #[cfg(feature = "services-s3")]
500        {
501            let chain =
502                reqsign_core::ProvideCredentialChain::<services::reqsign_aws_v4::Credential>::new();
503            let _ = services::S3::default().credential_provider_chain(chain);
504            assert!(schemes.contains("s3"));
505        }
506        #[cfg(not(feature = "services-s3"))]
507        assert!(!schemes.contains("s3"));
508
509        #[cfg(feature = "services-r2")]
510        {
511            let _ = services::R2::default();
512            assert!(schemes.contains("r2"));
513        }
514        #[cfg(not(feature = "services-r2"))]
515        assert!(!schemes.contains("r2"));
516
517        #[cfg(feature = "services-minio")]
518        {
519            let _ = services::Minio::default();
520            assert!(schemes.contains("minio"));
521        }
522        #[cfg(not(feature = "services-minio"))]
523        assert!(!schemes.contains("minio"));
524    }
525}
526
527/// Layers enabled through `layers-*` Cargo features.
528///
529/// A layer adds cross-service behavior to an [`Operator`]. Enable the matching
530/// facade feature, construct the layer, and pass it to [`Operator::layer`].
531/// Layer order is observable, so review each layer's composition notes when
532/// combining retries, timeouts, caching, or observability.
533pub mod layers {
534    pub use opendal_core::layers::*;
535    #[cfg(feature = "layers-aimd")]
536    pub use opendal_layer_aimd::*;
537    #[cfg(feature = "layers-async-backtrace")]
538    pub use opendal_layer_async_backtrace::*;
539    #[cfg(feature = "layers-await-tree")]
540    pub use opendal_layer_await_tree::*;
541    #[cfg(feature = "layers-cache")]
542    pub use opendal_layer_cache::{CacheDecision, CacheLayer, CachePolicy, CacheReadRequest};
543    #[cfg(feature = "layers-capability-check")]
544    pub use opendal_layer_capability_check::*;
545    #[cfg(feature = "layers-chaos")]
546    pub use opendal_layer_chaos::*;
547    #[cfg(feature = "layers-concurrent-limit")]
548    pub use opendal_layer_concurrent_limit::*;
549    #[cfg(all(target_os = "linux", feature = "layers-dtrace"))]
550    pub use opendal_layer_dtrace::*;
551    #[cfg(feature = "layers-fastmetrics")]
552    pub use opendal_layer_fastmetrics::*;
553    #[cfg(feature = "layers-fastrace")]
554    pub use opendal_layer_fastrace::*;
555    #[cfg(feature = "layers-foyer")]
556    pub use opendal_layer_foyer::*;
557    #[cfg(feature = "layers-hedge")]
558    pub use opendal_layer_hedge::*;
559    #[cfg(feature = "layers-hotpath")]
560    pub use opendal_layer_hotpath::*;
561    #[cfg(feature = "layers-immutable-index")]
562    pub use opendal_layer_immutable_index::*;
563    #[cfg(feature = "layers-logging")]
564    pub use opendal_layer_logging::*;
565    #[cfg(feature = "layers-metrics")]
566    pub use opendal_layer_metrics::*;
567    #[cfg(feature = "layers-mime-guess")]
568    pub use opendal_layer_mime_guess::*;
569    #[cfg(feature = "layers-otel-metrics")]
570    pub use opendal_layer_otelmetrics::*;
571    #[cfg(feature = "layers-otel-trace")]
572    pub use opendal_layer_oteltrace::*;
573    #[cfg(feature = "layers-prometheus")]
574    pub use opendal_layer_prometheus::*;
575    #[cfg(feature = "layers-prometheus-client")]
576    pub use opendal_layer_prometheus_client::*;
577    #[cfg(feature = "layers-retry")]
578    pub use opendal_layer_retry::*;
579    #[cfg(feature = "layers-route")]
580    pub use opendal_layer_route::*;
581    #[cfg(feature = "layers-sanity-check")]
582    pub use opendal_layer_sanity_check::*;
583    #[cfg(feature = "layers-tail-cut")]
584    pub use opendal_layer_tail_cut::*;
585    #[cfg(feature = "layers-throttle")]
586    pub use opendal_layer_throttle::*;
587    #[cfg(feature = "layers-timeout")]
588    pub use opendal_layer_timeout::*;
589    #[cfg(feature = "layers-tracing")]
590    pub use opendal_layer_tracing::*;
591}