Getting started
Your first program
This program builds an operator, writes a file, reads it back, inspects its metadata, and deletes it. It runs against the in-memory service with no credentials, so you can run it right after installing.
npm install opendal
import { Operator } from 'opendal'
async function main() {
// Configure a service, then build an operator from it.
const op = new Operator('memory')
// The same verbs work on every service.
await op.write('hello.txt', 'Hello, World!')
const bs = await op.read('hello.txt') // returns a Buffer
console.log(new TextDecoder().decode(bs))
const meta = await op.stat('hello.txt')
console.log(`size = ${meta.contentLength} bytes`)
await op.delete('hello.txt')
}
main()
write accepts a string or a Buffer; read returns a Buffer. Decode it
with new TextDecoder().decode(bs) or bs.toString().
Point it at a real backend
Only the service changes; the operations stay identical. The scheme is the first argument, and configuration is an options object:
import { Operator } from "opendal";
const op = new Operator("s3", {
bucket: "my-bucket",
region: "us-east-1",
});
await op.write("hello.txt", "Hello from S3!");
console.log((await op.read("hello.txt")).toString());
Connecting to your storage covers configuration and credentials. Services lists the backends and their configuration keys.
Build with additional services
The npm native packages enable services-all on Linux and macOS and
default on Windows. Both groups are listed in the binding's
Cargo.toml.
To add FTP to the Linux or macOS release service set, run these commands
from bindings/nodejs in a POSIX shell:
pnpm install --frozen-lockfile
NAPI_FEATURES=services-all,services-ftp pnpm build
On Windows, set $env:NAPI_FEATURES = "services-ftp" in PowerShell before
running pnpm build to add FTP to the default services.
The Node.js layers are built in. Exposing a new Rust layer requires Node.js binding code.
Async and sync
OpenDAL's Node.js API is promise-based: every operation returns a Promise you
await. For synchronous code, each verb has a *Sync variant that blocks and
returns the value directly:
import { Operator } from "opendal";
const op = new Operator("memory");
op.writeSync("hello.txt", "Hello, World!");
const bs = op.readSync("hello.txt");
console.log(bs.toString());
The sync variants cover the same operations (statSync, listSync,
deleteSync, …). Use the async API in servers and request handlers; reach for
the sync variants only in scripts and startup code where blocking is acceptable.