Opendal.Operator
Apache OpenDAL™ OCaml binding provides a unified data access layer that allows users to easily and efficiently retrieve data from various storage services.
(* Create an operator for local filesystem *)
let op =
Operator.new_operator "fs" [ ("root", "/tmp") ] |> Result.get_ok
in
(* Write data to a file *)
let _ = Operator.write op "hello.txt" (Bytes.of_string "Hello, World!") in
(* Read data back *)
let content = Operator.read op "hello.txt" |> Result.get_ok in
print_endline (String.of_bytes content)
val new_operator :
string ->
(string * string) list ->
(Opendal_core.Operator.operator, string) Stdlib.result
new_operator scheme config_map
creates a new blocking operator from given scheme and configuration.
@example
(* Local filesystem *)
let fs_op = new_operator "fs" [("root", "/tmp")] in
(* S3 storage *)
let s3_op = new_operator "s3" [
("bucket", "my-bucket");
("region", "us-east-1");
("access_key_id", "...");
("secret_access_key", "...")
] in
val list :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.entry array, string) Stdlib.result
list operator path
lists all entries in the given directory path.
Note: This loads all entries into memory. For large directories, consider using lister
for streaming access.
@example
match list op "data/" with
| Ok entries ->
Array.iter (fun entry ->
printf "Found: %s\n" (Entry.name entry)
) entries
| Error err -> printf "Error: %s\n" err
val lister :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.lister, string) Stdlib.result
lister operator path
creates a streaming lister for the given directory.
Use Lister.next
to iterate through entries one by one. This is memory-efficient for large directories.
@example
match lister op "data/" with
| Ok lst ->
let rec iter () =
match Lister.next lst with
| Ok (Some entry) ->
printf "Found: %s\n" (Entry.name entry);
iter ()
| Ok None -> () (* End of listing *)
| Error err -> printf "Error: %s\n" err
in iter ()
| Error err -> printf "Error: %s\n" err
val stat :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.metadata, string) Stdlib.result
stat operator path
gets metadata for the given path.
@example
match stat op "file.txt" with
| Ok meta ->
printf "Size: %Ld bytes\n" (Metadata.content_length meta);
printf "Is file: %b\n" (Metadata.is_file meta)
| Error err -> printf "Error: %s\n" err
val is_exist :
Opendal_core.Operator.operator ->
string ->
(bool, string) Stdlib.result
is_exist operator path
checks if the given path exists.
@example
match is_exist op "file.txt" with
| Ok true -> print_endline "File exists"
| Ok false -> print_endline "File does not exist"
| Error err -> printf "Error: %s\n" err
val create_dir :
Opendal_core.Operator.operator ->
string ->
(bool, string) Stdlib.result
create_dir operator path
creates a directory at the given path.
Notes:
@example
match create_dir op "data/subdir/" with
| Ok () -> print_endline "Directory created"
| Error err -> printf "Error: %s\n" err
val read :
Opendal_core.Operator.operator ->
string ->
(char array, string) Stdlib.result
read operator path
reads the entire file content into memory.
For large files or streaming access, consider using reader
.
@example
match read op "file.txt" with
| Ok content ->
let bytes = Array.to_seq content |> Bytes.of_seq in
print_endline (Bytes.to_string bytes)
| Error err -> printf "Error: %s\n" err
val reader :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.reader, string) Stdlib.result
reader operator path
creates a reader for streaming file access.
Use Reader.pread
to read data from specific positions.
@example
match reader op "file.txt" with
| Ok r ->
let buf = Bytes.create 1024 in
(match Reader.pread r buf 0L with
| Ok bytes_read -> printf "Read %d bytes\n" bytes_read
| Error err -> printf "Error: %s\n" err)
| Error err -> printf "Error: %s\n" err
val write :
Opendal_core.Operator.operator ->
string ->
bytes ->
(unit, string) Stdlib.result
write operator path data
writes data to the given path.
Notes:
@example
let data = Bytes.of_string "Hello, World!" in
match write op "hello.txt" data with
| Ok () -> print_endline "File written"
| Error err -> printf "Error: %s\n" err
val writer :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.writer, string) Stdlib.result
writer operator path
creates a writer for streaming file writes.
Use Writer.write
to write data chunks and Writer.close
to finalize.
@example
match writer op "large_file.txt" with
| Ok w ->
let _ = Writer.write w (Bytes.of_string "chunk1") in
let _ = Writer.write w (Bytes.of_string "chunk2") in
Writer.close w
| Error err -> printf "Error: %s\n" err
val copy :
Opendal_core.Operator.operator ->
string ->
string ->
(unit, string) Stdlib.result
copy operator from to
copies a file from source to destination.
Notes:
@example
match copy op "source.txt" "backup.txt" with
| Ok () -> print_endline "File copied"
| Error err -> printf "Error: %s\n" err
val rename :
Opendal_core.Operator.operator ->
string ->
string ->
(unit, string) Stdlib.result
rename operator from to
renames/moves a file from source to destination.
Notes:
@example
match rename op "old_name.txt" "new_name.txt" with
| Ok () -> print_endline "File renamed"
| Error err -> printf "Error: %s\n" err
val delete :
Opendal_core.Operator.operator ->
string ->
(unit, string) Stdlib.result
delete operator path
deletes the file at the given path.
Notes:
remove_all
instead)@example
match delete op "unwanted.txt" with
| Ok () -> print_endline "File deleted"
| Error err -> printf "Error: %s\n" err
val remove :
Opendal_core.Operator.operator ->
string array ->
(unit, string) Stdlib.result
remove operator paths
deletes multiple files in a batch operation.
This is more efficient than calling delete
multiple times for services that support batch deletion.
@example
let files = [|"file1.txt"; "file2.txt"; "file3.txt"|] in
match remove op files with
| Ok () -> print_endline "Files deleted"
| Error err -> printf "Error: %s\n" err
val remove_all :
Opendal_core.Operator.operator ->
string ->
(unit, string) Stdlib.result
remove_all operator path
recursively deletes the directory and all its contents.
⚠️ WARNING: This operation permanently deletes all files and subdirectories. Use with extreme caution!
@example
match remove_all op "temp_data/" with
| Ok () -> print_endline "Directory deleted"
| Error err -> printf "Error: %s\n" err
val check : Opendal_core.Operator.operator -> (unit, string) Stdlib.result
check operator
performs a health check on the storage service.
This verifies that the operator can connect to and access the configured storage service.
@example
match check op with
| Ok () -> print_endline "Storage service is accessible"
| Error err -> printf "Error: %s\n" err
info operator
returns information about the operator.
@example
let info = info op in
printf "Service: %s\n" (OperatorInfo.name info);
printf "Scheme: %s\n" (OperatorInfo.scheme info);
printf "Root: %s\n" (OperatorInfo.root info)
module Reader : sig ... end
module Writer : sig ... end
module Lister : sig ... end
module Metadata : sig ... end
module Entry : sig ... end
module OperatorInfo : sig ... end
module Capability : sig ... end