Skip to content

内存中的文件 ​

并不是所有要发送的内容都存在磁盘上。writeFile() 和 readFile() 直接在内存和远程文件之间传输数据,SFTP 和 SCP 都支持。

写入远程文件 ​

ts
// 字符串或字节数据。
await client.writeFile('/etc/motd', 'Welcome to prod\n');
await client.writeFile('/srv/app/config.json', JSON.stringify(config, null, 2), { mode: 0o600 });

// 流,适合大到无法放进内存的数据。
import { createReadStream, statSync } from 'node:fs';

const file = './backup.tar.gz';
await client.writeFile('/srv/backups/latest.tar.gz', createReadStream(file), {
  size: statSync(file).size,
});
选项默认值含义
mode0o644新建文件的权限。
size流的长度,单位为字节。
signal取消写入。

通过 SCP 写入流时需要指定 size

SCP 协议要在发送数据之前声明文件大小。如果没有 size,node-scp 会先把整个流读入内存。指定了 size 时,无论使用哪种协议,流的长度都必须与之完全一致,否则写入会失败。

读取远程文件 ​

readFile() 返回一个包含全部内容的 Buffer:

ts
const raw = await client.readFile('/srv/app/config.json');
const config = JSON.parse(raw.toString('utf8'));

对于大文件,请改用 download() 保存到磁盘:它以流的方式传输,并且会报告进度。

示例:修改远程配置文件 ​

ts
const path = '/etc/config/system';
const current = (await client.readFile(path)).toString();
const updated = current.replace(/option hostname '.*'/, "option hostname 'office-router'");
if (updated !== current) await client.writeFile(path, updated);

基于 MIT 许可证发布。