Common tasks
Recipes for the things you actually do with storage. Each works on every
service. They assume an Operator op built as in
Getting started. For full signatures, see the
API reference.
A few conventions used throughout:
- Paths are relative to the operator's root; a trailing
/means a directory (logs/app/is a directory,logs/appis a file). readreturnsbyte[];writetakes aStringorbyte[].- The synchronous
Operatoris shown here.AsyncOperatormirrors these verbs, returning aCompletableFuturefrom each call.
Read a whole file
byte[] data = op.read("path/to/file");
String text = new String(data);
Read part of a file
read(path, offset, length) reads a range:
byte[] data = op.read("path/to/file", 0, 1024);
Pass ReadOptions to op.read(path, options) or
op.createInputStream(path, options) to configure a single read. Async reads accept
the same options:
import org.apache.opendal.ReadOptions;
ReadOptions options = ReadOptions.builder()
.offset(1024)
.length(4096)
.ifMatch("\"etag\"")
.chunk(2048)
.concurrent(2)
.build();
byte[] data = op.read("path/to/file", options);
ReadOptions supports the version and condition fields described below for
ReaderOptions. Use chunk to set the size of each read request and
concurrent to limit parallel requests.
contentLengthHint specifies the total object size in bytes, not the length of
the requested range. It can avoid a metadata request when OpenDAL needs the
object length to plan a read. An incorrect hint may cause incomplete reads or
errors.
For repeated reads from the same file, create an OperatorReader. It uses a
Rust core reader and lets each call select an independent byte range:
import org.apache.opendal.OperatorReader;
import org.apache.opendal.ReaderOptions;
ReaderOptions options = ReaderOptions.builder()
.chunk(8 * 1024 * 1024L)
.concurrent(4)
.prefetch(2)
.build();
try (OperatorReader reader = op.createReader("path/to/file", options)) {
byte[] first = reader.read(0, 1024);
byte[] next = reader.read(1024, 1024);
}
A positive chunk enables internal range requests, and concurrent limits
how many run in parallel. Setting concurrent alone keeps unchunked streaming.
During concurrent chunked reads, prefetch lets up to that many finished
reads free slots for additional requests before OpenDAL collects their
results. The default, 0, disables these extra slots. Reads already
submitted can still finish and wait to be collected. Serial reads collect
each result before starting the next read. Higher values can increase memory
use.
A reader does not snapshot the file, so subsequent reads may observe changes.
ReaderOptions omits gap because Java does not expose multi-range reads.
ReaderOptions also supports version, ifMatch, ifNoneMatch,
ifVersionMatch, ifVersionNotMatch, ifModifiedSince, and
ifUnmodifiedSince. The time conditions accept java.time.Instant.
Version selection and conditions require service support. Unsupported options
fail with Unsupported; a failed condition on an existing file fails with
ConditionNotMatch. Conditions apply to all requests through the reader,
including its streams. Errors may surface when creating the reader
or while reading from it.
Stream a large file
Don't load gigabytes into memory — read through an InputStream in chunks:
import java.io.InputStream;
try (InputStream in = op.createInputStream("big.bin")) {
byte[] buffer = new byte[8192];
int n;
while ((n = in.read(buffer)) != -1) {
// process buffer[0..n]
}
}
For a configured reader, use reader.createInputStream() for the whole file
or reader.createInputStream(offset, length) for a range. A length of -1
reads to the end; zero selects an empty range. Each stream has its own cursor.
Close each reader and stream separately: closing a reader does not close its
streams, and closing the operator does not close its readers.
Write a whole file
op.write("path/to/file", "Hello, World!");
op.write("path/to/file", new byte[] {0, 1, 2, 3});
Stream a large upload
Write incrementally through an OutputStream; closing it commits the upload:
import java.io.OutputStream;
try (OutputStream out = op.createOutputStream("big.bin")) {
out.write(firstChunk);
out.write(secondChunk);
}
When copying from an input stream, a source failure still leaves the output
stream open. Try-with-resources then closes it and can commit the prefix
already written. To abandon the upload, catch the failure inside the resource
block and call OperatorOutputStream.abort() before automatic close. Keep the
original failure, including unchecked cancellation exceptions, and attach any
abort failure as a suppressed exception. This example uses transferTo from
JDK 9.
import java.io.InputStream;
import java.nio.file.Files;
import org.apache.opendal.OperatorOutputStream;
try (InputStream in = Files.newInputStream(source);
OperatorOutputStream out = op.createOutputStream("big.bin")) {
try {
in.transferTo(out);
} catch (Throwable failure) {
try {
out.abort();
} catch (Throwable cleanupFailure) {
if (cleanupFailure != failure) {
failure.addSuppressed(cleanupFailure);
}
}
throw failure;
}
}
abort() discards Java-side buffered bytes and asks the storage backend to
clean up the unfinished write. The stream closes even if cleanup fails, so
automatic close() never commits after abort. Inspect suppressed exceptions
for cleanup failures. Cleanup depends on the backend and can leave already
visible data in place. Restoring a previous object requires backend-specific
support.
Check metadata
import org.apache.opendal.Metadata;
Metadata meta = op.stat("path/to/file");
System.out.println(meta.getContentLength() + " bytes, dir = " + meta.isDir());
List a directory
list returns the entries under a path:
import org.apache.opendal.Entry;
for (Entry entry : op.list("dir/")) {
System.out.println(entry.getPath() + " " + entry.getMetadata().getMode());
}
Walk a tree recursively
Pass a ListOptions with recursive(true):
import org.apache.opendal.Entry;
import org.apache.opendal.ListOptions;
ListOptions options = ListOptions.builder().recursive(true).build();
for (Entry entry : op.list("dir/", options)) {
System.out.println(entry.getPath());
}
Delete a file or a whole tree
import org.apache.opendal.DeleteOptions;
op.delete("path/to/file"); // single path; succeeds even if missing
op.delete("a/", DeleteOptions.builder().recursive(true).build());
Both Operator and AsyncOperator accept DeleteOptions. Use the builder to
select a stored version or set conditions based on the current file's ETag,
version, or previously returned metadata. Option support depends on the service.
A failed condition returns ConditionNotMatch and an unsupported option returns
Unsupported.
Pass a trailing slash to limit recursive deletion to the directory. On
prefix-based services, "a" can also match entries under "ab/". You can also
use op.removeAll("a/") for recursive deletion.
Create a directory
op.createDir("path/to/dir/"); // the trailing slash is required
Copy and rename
op.copy("from.txt", "to.txt");
op.rename("old.txt", "new.txt");
Both require a service that supports them; see capability checks.
Generate a presigned URL
Hand a time-limited URL to a third party so they can access an object without your credentials. Presigning is part of the async API:
import java.time.Duration;
import org.apache.opendal.AsyncOperator;
import org.apache.opendal.PresignedRequest;
try (AsyncOperator op = AsyncOperator.of("s3", conf)) {
PresignedRequest req =
op.presignRead("path/to/file", Duration.ofHours(1)).join();
System.out.println(req.getMethod() + " " + req.getUri());
System.out.println(req.getHeaders());
}
presignWrite and presignStat produce presigned requests for the matching
operations.
Pass ReadOptions to set response headers, for example to give a download a
filename:
import org.apache.opendal.ReadOptions;
try (AsyncOperator op = AsyncOperator.of("s3", conf)) {
ReadOptions options = ReadOptions.builder()
.overrideContentDisposition("attachment; filename=\"report.csv\"")
.overrideContentType("text/csv")
.overrideCacheControl("private, max-age=60")
.build();
PresignedRequest req =
op.presignRead("reports/latest.csv", Duration.ofHours(1), options).join();
}
presignStat accepts StatOptions, and presignWrite accepts WriteOptions.
The overloads without options retain their existing behavior. Support for each
option depends on the storage service. Send the returned method and headers
along with the URI when using the presigned request.