读取文件
您可以使用files.read()
方法从沙箱文件系统中读取文件。
Copy
import { Sandbox } from 'ppio-sandbox/code-interpreter'
const sandbox = await Sandbox.create()
// 在沙箱内创建一个文件用于测试
const filePathInSandbox = '/tmp/test-file'
await sandbox.files.write(filePathInSandbox, "test-file-content")
const fileContent = await sandbox.files.read(filePathInSandbox)
console.log(fileContent)
await sandbox.kill()
写入单个文件
您可以使用files.write()
方法将单个文件写入沙箱文件系统。
Copy
import { Sandbox } from 'ppio-sandbox/code-interpreter'
const sandbox = await Sandbox.create()
const filePathInSandbox = '/tmp/test-file'
const result = await sandbox.files.write(filePathInSandbox, "test-file-content")
console.log(result)
await sandbox.kill()
写入多个文件
您也可以使用files.write()
方法将多个文件写入沙箱文件系统。
Copy
import { Sandbox } from 'ppio-sandbox/code-interpreter'
const sandbox = await Sandbox.create()
const result = await sandbox.files.write([
{ path: '/tmp/test-file-1', data: 'file content 1' },
{ path: '/tmp/test-file-2', data: 'file content 2' }
])
console.log(result)
await sandbox.kill()