SFTP 客户端
100% Typescription SFTP 客户端
函数参考
类别 SFTPClient
SFTPClient 类提供了所有功能,可以与 SFTP服务员. 您可以使用密码或私钥连接到服务器 .
示例:使用密码从 sftp 服务器获取文件
import SFTPClient from '@distributed-systems/sftp-client';
// represents one connection to the server
const client = new SFTPClient();
// conenct using a password
await client.connect({
hostname: 'l.dns.porn',
port: 22,
username: 'linaGirl',
password: 'totally-secure',
});
// get the file as a buffer
const file = await client.getFile('/home/linaGirl/nice-file.dat');
client.end();
SFTPClient. 连接(证书)
这个 connect 方法用于打开与服务器的连接。 它接受一个
选项对象,包含所需的证书.
您可以使用密码或私钥连接 。 你可以用两个 同时使用方法。 将使用第一个成功的方法 那麽.
返回 sftpClient 实例 .
使用密码连接
await client.connect({
hostname: 'l.dns.porn',
port: 22,
username: 'linaGirl',
password: 'totally-secure',
});
使用私钥连接
import fs from 'fs';
const privateKey = await fs.promises.readFile('/home/linaGirl/.ssh/id_rsa');
await client.connect({
hostname: 'l.dns.porn',
port: 22,
username: 'linaGirl',
privateKey: privateKey,
});
SFTPClient.create Brief (DirectoryPath, recursive = false) (中文(简体) )
在服务器上创建目录。 如果 recursive
参数设置为 true。 。 。 .
返回 sftpClient 实例 .
创建目录
await client.createDirectory('best-directory-ever');
创建整个路径
await client.createDirectory('best-directory-ever/awesome-directory', true);
SFTPClient.create. Read Stream (filePath) (中文(简体) )
使用可读流下载文件。 溪流的 API 与 标准 节点.js 可读流。这是您应当使用的方法 以下载文件到文件系统.
返回可读流.
下载文件到文件系统
import fs from 'fs';
// create the readable stream for the file you like to download
const readStream = await client.createReadStream('fancy-file.fancy');
// create a write stream for the file the data should be stored in
const writeStream = fs.createWriteStream('/home/linaGirl/fancy-file.fancy');
// pipe the stream
readStream.pipe(writeStream);
// get notified when the stream has finished
writeStream.on('close', () => {
console.log('the file was successfully saved!');
});
创建文件的 SHA1 校验和
import crypto from 'crypto';
const hash = crypto.createHash('sha1');
// create the readable stream for the file you like to hash
const readStream = await client.createReadStream('fancy-file.fancy');
// feed the data to the hash
readStream.on('data', (buffer) => {
hash.update(buffer);
});
// log the checksum
readStream.on('close', () => {
console.log(hash.digest('hex'));
});
SFTPClient.createWriteStream (filePath) (中文(简体) )
使用可写流上传文件。 溪流的 API 与 标准 节点.js 可写流。 。 。 .
返回可写流 .
从文件系统下载文件
import fs from 'fs';
// create the writable stream for the file you like to upload
const writeStream = await client.createReadStream('fancy-file.fancy');
// create a read stream for the file the data should be read from
const readStream = fs.createReadStream('/home/linaGirl/fancy-file.fancy');
// pipe the stream
readStream.pipe(writeStream);
// get notified when the stream has finished
writeStream.on('close', () => {
console.log('the file was successfully uploaded!');
});
SFTPClient.delete Directory (DirectoryPath, 递归=假) (中文(简体) )
删除服务器上的目录。 可连续删除所有包含的文件
和目录,如果 recursive 参数设置为 true。 。 。 .
返回 sftpClient 实例 .
删除目录
await client.deleteDirectory('best-directory-ever');
递归删除目录
await client.deleteDirectory('best-directory-ever', true);
SFTPClient.deleteFile (filePath) (英语)
删除一个文件.
返回 sftpClient 实例 .
删除文件
await client.deleteFile('/path/to/file/that/needs/to.go');
SFTPClient. end () (中文(简体) )
结束服务器的连接.
返回 sftpClient 实例 .
结束连接
await client.end();
SFTPClient. 存在(路径)
检查文件或目录是否存在.
返回布尔值 .
检查文件是否存在
const fileExists = await client.exists('path/to/file');
检查目录是否存在
const fileExists = await client.exists('path/to/directory');
SFTPClient. 获得 文件 (文件路径)
将文件下载到缓冲器中.
返回包含所请求文件的缓冲器 .
下载文件
const fileBuffer = await client.getFile('/path/to.file');
SFTPClient.list (路径, 详细 = 虚假)
列出指定的路径。 路径可能是文件或目录.
返回包含文件名称的数组或包含对象的数组 详细文件信息.
列出目录的文件
const filenames = await client.list('path/to/directory');
console.log(filenames);
// ['a.mjs', 'directory', 'z.jpg']
列出目录文件, 返回细节
const files = await client.list('path/to/directory', true);
console.log(files);
[{filename: 'a.mjs', stats: StatsObject}]
见 类数 对于目标的内容
SFTPClient.move (源代码,目标代码)
将文件或目录从 sourcePath 页:1 targetPath。可用于
以重命名文件和目录.
返回 sftpClient 实例 .
重命名文件
await client.move('nice-code.mjs', 'legacy-code.mjs');
移动目录
await client.move('directory', 'some/path/for/the/directory');
SFTPClient.put (英语). 文件( 文件 Path, 数据缓冲)
从缓冲器上传文件.
返回 sftpClient 实例 .
上传文件
await client.putFile('/path/to.file', buffer);
SFTPClient.set 许可(文件Path,权限)
在文件中设置权限。 如果您需要获得文件的权限 能够 统计 这个!
返回 sftpClient 实例 .
在文件中设置权限
import { Permissions } from '@distributed-systems/sftp-client';
// give the user read permissions
const permissions = new Permissions();
permissions.user.read = true;
await client.setPermissions('path/to/file', permissions);
见 类权限 关于权限对象的细节.
SFTPClient.stat (路径)
获取有关文件的数据。 报告文件的类型及其权限.
返回实例 点级。 。 。 .
获取文件数据
const stats = await client.stat('path/to/file');
见 类数 用于统计对象的内容.
类权限
权限类保存关于文件系统权限的信息 对象。 它可用于获得对象的类型. 也可以用来读 或写入对象的权限.
权限对象作为属性返回 数据 对象 统计 编辑 列表 打电话. 它可以传递给 设置 调用更改对象的权限.
对象会显示权限属性 user,则 group 用于
other 每一个属性都是具有三种可写入属性的对象
显示 read, (中文(简体) ), write 财务报告和审定财务报表 execute 权限.
给大家所有许可
import { Permissions } from '@distributed-systems/sftp-client';
const permissions = new Permissions();
permissions.user.read = true;
permissions.user.write = true;
permissions.user.execute = true;
permissions.group.read = true;
permissions.group.write = true;
permissions.group.execute = true;
permissions.other.read = true;
permissions.other.write = true;
permissions.other.execute = true;
await client.setPermissions('path/to/file', permissions);
检查用户在文件中是否有写入权限
const stats = await client.stat('path/to/file');
console.log(stats.permissions.user.write); // true or false
类数
对象的属性包含 权限 对象和一系列方法 用于确定 stat 调用路径的类型.
使用统计方法获取数据
const stats = await client.stat('path/to/file');
Stats.is BlockDevice () (英语)
回返 true 如果路径是一个块设备, false 否则.
检查路径是否为块设备
const stats = await client.stat('path/to/file/or/directory');
console.log(stats.isBlockDevice()); // true or false
Stats.is 沙拉克特·德维塞()
回返 true 如果路径是一个字符设备, false 否则.
检查路径是否为字符设备
const stats = await client.stat('path/to/file/or/directory');
console.log(stats.isCharacterDevice()); // true or false
Stats.is 导演( )
回返 true 如果路径是一个目录, false 否则.
检查路径是否为目录
const stats = await client.stat('path/to/file/or/directory');
console.log(stats.isDirectory()); // true or false
Stats.isFIFO () (中文(简体) )
回返 true 如果这条道是一条命名的管子, false 否则.
检查路径是否是一个命名的管道
const stats = await client.stat('path/to/file/or/directory');
console.log(stats.isFIFO()); // true or false
Stats.isFile () (中文(简体) )
回返 true 如果路径是普通文件, false 否则.
检查路径是否为正则文件
const stats = await client.stat('path/to/file/or/directory');
console.log(stats.isFile()); // true or false
数据库(Socket)
回返 true 如果路径是插座, false 否则.
检查路径是否为套接字
const stats = await client.stat('path/to/file/or/directory');
console.log(stats.isSocket()); // true or false
Stats.is SymbolicLink () (中文(简体) )
回返 true 如果路径是象征性的链接, false 否则.
检查路径是否是一个符号链接
const stats = await client.stat('path/to/file/or/directory');
console.log(stats.isSymbolicLink()); // true or false