Opendal.OperatorApache 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.resultnew_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", "...")
] inval list :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.entry array, string) Stdlib.resultlist 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" errval lister :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.lister, string) Stdlib.resultlister 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" errval stat :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.metadata, string) Stdlib.resultstat 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" errval is_exist :
Opendal_core.Operator.operator ->
string ->
(bool, string) Stdlib.resultis_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" errval create_dir :
Opendal_core.Operator.operator ->
string ->
(bool, string) Stdlib.resultcreate_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" errval read :
Opendal_core.Operator.operator ->
string ->
(char array, string) Stdlib.resultread 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" errval reader :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.reader, string) Stdlib.resultreader 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" errval write :
Opendal_core.Operator.operator ->
string ->
bytes ->
(unit, string) Stdlib.resultwrite 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" errval writer :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.writer, string) Stdlib.resultwriter 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" errval copy :
Opendal_core.Operator.operator ->
string ->
string ->
(unit, string) Stdlib.resultcopy 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" errval rename :
Opendal_core.Operator.operator ->
string ->
string ->
(unit, string) Stdlib.resultrename 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" errval delete :
Opendal_core.Operator.operator ->
string ->
(unit, string) Stdlib.resultdelete 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" errval remove :
Opendal_core.Operator.operator ->
string array ->
(unit, string) Stdlib.resultremove 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" errval remove_all :
Opendal_core.Operator.operator ->
string ->
(unit, string) Stdlib.resultremove_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" errval check : Opendal_core.Operator.operator -> (unit, string) Stdlib.resultcheck 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" errinfo 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 ... endmodule Writer : sig ... endmodule Lister : sig ... endmodule Metadata : sig ... endmodule Entry : sig ... endmodule OperatorInfo : sig ... endmodule Capability : sig ... end