跳转到内容
返回主页

文件读写与上传下载

文件系统操作通过沙箱运行时 API 执行。SDK 会隐藏运行时鉴权、Connect RPC Header 以及上传/下载路由差异。

await sandbox.files.write("/root/workspace/hello.txt", "hello\n");
const text = await sandbox.files.read("/root/workspace/hello.txt");
console.log(text);
await sandbox.files.makeDir("/root/workspace/app");
await sandbox.files.write("/root/workspace/app/index.html", "<h1>Hello</h1>");
const entries = await sandbox.files.list("/root/workspace/app");
console.log(entries);

快速迭代时,可由应用读取本地文件,再写入沙箱:

const html = await fs.promises.readFile("./index.html", "utf8");
await sandbox.files.write("/root/workspace/frontend/index.html", html);

如果源码和依赖准备需要复用,优先使用已准备好的官方模板或固定具体 tpl-...。具备模板构建权限的项目也可以通过模板构建流程预置依赖。

优先使用 SDK helper。调试原始运行时流量时,常见路由如下:

路由用途
GET {envdUrl}/file?path=<path>读取原始文件字节。
POST {envdUrl}/file?path=<path>写入原始文件字节。
GET {envdUrl}/files/content?path=<path>以结构化响应读取文件内容。
POST {envdUrl}/files上传 multipart 内容。
POST {envdUrl}/files/batch批量写入文件。
POST {envdUrl}/files/compose从多个源文件组合输出。

所有路由都需要 X-Access-Token: <envdAccessToken>

目标 SDK/运行时开启后,可通过运行时文件系统能力监听目录。当 Agent 写入输出文件时,编排方可以据此响应。

const watcher = await sandbox.files.watchDir("/root/workspace/output", {
recursive: true,
});
for await (const event of watcher) {
console.log(event.type, event.path);
}
规则原因
优先使用绝对路径避免模板 workdir 带来的歧义。
ephemeral 视为沙箱生命周期存储删除沙箱后文件消失。
不把产物写入日志生成文件可能包含密钥或用户数据。
存储类型持久化行为适用场景
ephemeral仅在沙箱生命周期内存在。可丢弃临时文件和生成产物。