Skip to content

Files

Write, read, list, move and delete files in a sandbox.

Every sandbox has a file system you can use from your code without running a process. Relative paths are in the sandbox’s home folder.

import { AnchorClient } from "@mistrive/anchor";

const client = new AnchorClient({
  baseUrl: "https://anchor.jgcp.mistrivedev.com",
  token: process.env.SUTRA_API_KEY,
  project: process.env.SUTRA_PROJECT,
});

const sandbox = await client.create();

// Relative paths are in the home folder. A string source is a local file
// path, so pass the content as a Buffer.
await sandbox.fs.writeObject(Buffer.from("draft\n"), "notes/todo.txt", { createParents: true });
await sandbox.fs.move("notes/todo.txt", "notes/done.txt");

const { files } = await sandbox.fs.listFiles("notes");
console.log(files.map((f) => f.path));

const content = await sandbox.fs.downloadFile("notes/done.txt");
console.log(content.toString());

await sandbox.fs.deleteFile("notes", { recursive: true });
await sandbox.delete();
import os

from mistrive_anchor import AnchorClient

client = AnchorClient(
    base_url="https://anchor.jgcp.mistrivedev.com",
    token=os.environ["SUTRA_API_KEY"],
    project=os.environ["SUTRA_PROJECT"],
)

sandbox = client.create()

# Relative paths are in the home folder. A str source is a local file path,
# so pass the content as bytes.
sandbox.fs.write_object(b"draft\n", "notes/todo.txt", {"createParents": True})
sandbox.fs.move("notes/todo.txt", "notes/done.txt")

listing = sandbox.fs.list_files("notes")
print([f.path for f in listing.files])

print(sandbox.fs.download_file("notes/done.txt").decode())

sandbox.fs.delete_file("notes", {"recursive": True})
sandbox.delete()
#!/usr/bin/env bash
set -euo pipefail

# Assumes you ran `anchor auth login` with a project (see the Quickstart).
# Today the id of a deleted sandbox cannot be used again, so pick a new one.
sandbox="demo-$(date +%s)"
anchor sandbox create --name "$sandbox"
printf 'draft\n' > todo.txt

# Relative paths are in the home folder.
anchor sandbox fs upload "$sandbox" todo.txt notes/todo.txt --create-parents
anchor sandbox fs mv "$sandbox" notes/todo.txt notes/done.txt
anchor sandbox fs list "$sandbox" notes
anchor sandbox fs download "$sandbox" notes/done.txt done.txt
anchor sandbox fs delete "$sandbox" notes --recursive

anchor sandbox delete "$sandbox"
import { Sutra } from "@mistrive/sutra";

const sutra = new Sutra({
  baseUrl: "https://anchor.jgcp.mistrivedev.com",
});

const sandbox = await sutra.sandboxes.create();

// Relative paths are in the home folder.
await sandbox.files.mkdir("notes");
await sandbox.files.write("notes/todo.txt", "draft\n");
await sandbox.files.move("notes/todo.txt", "notes/done.txt");

const page = await sandbox.files.list("notes");
console.log(page.items.map((f) => f.path));

console.log(await sandbox.files.readText("notes/done.txt"));

// Download a folder as a tar archive.
const archive = await sandbox.files.downloadArchive(["notes"]);
console.log(archive.byteLength);

await sandbox.files.delete("notes", { recursive: true });
await sandbox.delete();
from mistrive.sutra import Sutra

sutra = Sutra(
    base_url="https://anchor.jgcp.mistrivedev.com",
)

sandbox = sutra.sandboxes.create()

# Relative paths are in the home folder.
sandbox.files.mkdir("notes")
sandbox.files.write("notes/todo.txt", "draft\n")
sandbox.files.move("notes/todo.txt", "notes/done.txt")

page = sandbox.files.list("notes")
print([f.path for f in page.items])

print(sandbox.files.read_text("notes/done.txt"))

# Download a folder as a tar archive.
archive = sandbox.files.download_archive(["notes"])
print(len(archive))

sandbox.files.delete("notes", recursive=True)
sandbox.delete()
#!/usr/bin/env bash
set -euo pipefail

# Assumes you ran `sutra auth login` (see the Quickstart).
sutra sandboxes create demo

# Relative paths are in the home folder.
sutra files mkdir demo notes
echo "draft" | sutra files write demo notes/todo.txt
sutra files move demo notes/todo.txt notes/done.txt
sutra files list demo notes
sutra files read demo notes/done.txt
sutra files download-archive demo notes > notes.tar
sutra files delete demo notes --recursive

sutra sandboxes delete demo

What you can do

Task Call
Write a file write
Read a file read for bytes, readText for text
List a folder list, with page tokens for large folders
Get the size, type and mode of a path info
Create a folder mkdir
Move or rename move
Copy copy
Delete delete, with recursive for a folder
Upload many files at once uploadArchive, which unpacks a tar archive into a folder
Download several paths downloadArchive, which returns a tar archive

The calls work while the sandbox is running. Files on the sandbox’s disk last as long as the sandbox does, unless it is stopped and keeps only its volumes. To keep files beyond that, write them to a volume.

Type to search…

↑↓ navigate↵ selectEsc close