使用 CLI 可以连接到已经运行的云沙箱,并打开交互式终端:
e2b sandbox connect <sandbox-id>连接成功后,可以像使用普通远程终端一样执行命令:
ls
pwd
exit与 e2b sandbox create <template> 不同,connect 只连接已有沙箱。退出终端只会关闭本地终端会话,不会自动终止沙箱。
如果需要在服务端代码中连接已有沙箱,请使用 SDK:
const sandbox = await Sandbox.connect("<sandbox-id>", {
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
});Python 示例:
sandbox = Sandbox.connect(
"<sandbox-id>",
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)该文章对您有帮助吗?