Skip to main content

Getting started

Prerequisites

Build the D binding from source before running any code. From bindings/d/:

OPENDAL_TEST=memory dub build -b release

See Overview for the full build requirements.

Your first program

The example below creates an Operator against the in-memory backend (no credentials needed), writes a file, reads it back, checks that it exists, inspects its metadata, and deletes it.

import opendal;
import std.stdio : writeln;

void main() @safe
{
// Build an OperatorOptions, then construct the Operator.
auto options = new OperatorOptions();
auto op = Operator("memory", options);

// Write bytes to a path.
string data = "Hello, OpenDAL!";
op.write("hello.txt", cast(ubyte[]) data.dup);

// Read them back.
ubyte[] result = op.read("hello.txt");
writeln("read ", result.length, " bytes");

// Inspect metadata.
auto meta = op.stat("hello.txt");
writeln("size = ", meta.contentLength(), " bytes");

// Delete the file.
op.remove("hello.txt");
}

Listing a directory

createDir and list work together:

import opendal;
import std.stdio : writeln;

void main() @safe
{
auto options = new OperatorOptions();
auto op = Operator("memory", options);

op.createDir("logs/");
op.write("logs/a.txt", cast(ubyte[]) "first".dup);
op.write("logs/b.txt", cast(ubyte[]) "second".dup);

Entry[] entries = op.list("logs/");
foreach (e; entries)
writeln(e.path(), " name=", e.name());
}

Pointing at a real backend

Only the OperatorOptions change; all method calls stay identical.

import opendal;

void main() @safe
{
auto options = new OperatorOptions();
options.set("root", "/tmp/mydata");

auto op = Operator("fs", options);
op.write("hello.txt", cast(ubyte[]) "Hello from the filesystem!".dup);
}

OPENDAL_TEST is read by the build script at build time to compile the named service into the C library. To use the fs backend you must build with it enabled:

OPENDAL_TEST=fs OPENDAL_FS_ROOT=/tmp/mydata dub build -b release

At runtime the backend is selected by the scheme string passed to Operator (e.g. "fs"), and backend-specific settings are passed through OperatorOptions (e.g. options.set("root", "/tmp/mydata")). See Services for every available backend and its configuration keys.

Parallel operations

Pass useParallel = true to spawn a TaskPool and use writeParallel, readParallel, and listParallel:

import opendal;

void main() @safe
{
auto options = new OperatorOptions();
auto op = Operator("memory", options, true /* useParallel */);

op.writeParallel("hello.txt", cast(ubyte[]) "data".dup);
ubyte[] result = op.readParallel("hello.txt");
}