# @distributed-systems/sftp-client 中文文档

规范地址：https://npmcn.com/package/%40distributed-systems/sftp-client/

版本：2\.0\.0
npm 元数据更新时间：2024-09-22T08:30:40.586Z
中文译文更新时间：2026-11-12T12:18:26.261Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40distributed-systems/sftp-client
原文对照：https://npmcn.com/package/%40distributed-systems/sftp-client/?lang=original
项目主页：https://github.com/distributed-systems/sftp-client#readme
源码仓库：https://github.com/distributed-systems/sftp-client

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：100 kB，30 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：4 个
- 首次发布：2018-11-09T11:45:18.254Z
- 最新版本发布：2024-09-22T08:30:40.381Z
- 近 30 天下载量：4173（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：7（近 12 个月 0 个）

## 中文简介

简单、充分测试和可靠的 SFTP 客户端

## 安装

```sh
npm install @distributed-systems/sftp-client
```

也可以使用：`pnpm add @distributed-systems/sftp-client`、`yarn add @distributed-systems/sftp-client`、`bun add @distributed-systems/sftp-client`

## README

# SFTP 客户端

100% Typescription SFTP 客户端

## 函数参考

-   [类别 SFTPClient](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-sftpclient)
    -   [连接(证书)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientconnect-credentials)
    -   [创建目录( DirectoryPath, 递归=假)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientcreatedirectory-directorypath-recursive--false)
    -   [创建ReadStream( 文件盘)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientcreatereadstream-filepath)
    -   [创建WriteStream( 文件Path)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientcreatewritestream-filepath)
    -   [删除标题( 方向盘, 递归=假)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientdeletedirectory-directorypath-recursive--false)
    -   [删除文件( 文件Path)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientdeletefile-filepath)
    -   [结束( )](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientend-)
    -   [存在 (路径)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientexists-path)
    -   [获取文件( 文件Path)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientgetfile-filepath)
    -   [列表(路径,详细=虚假)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientlist-path-detailed--false)
    -   [移动( 源代码、 目标代码)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientmove-sourcepath-targetpath)
    -   [putFile( 文件 Path, 数据缓冲)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientputfile-filepath-databuffer)
    -   [设定( 文件 Path, 权限)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientsetpermissions-filepath-permissions)
    -   [stat (路径)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientstat-path)
-   [类权限](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-permissions)
-   [类数](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-stats)
    -   [是 BlockDevice () (中文(简体) )](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-statsisblockdevice-)
    -   [切开器 Device ()](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-statsischaracterdevice-)
    -   [是源代码 ()](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-statsisdirectory-)
    -   [isFIFO () (中文(简体) )](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-statsisfifo-)
    -   [单数 ()](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-statsisfile-)
    -   [isSocket () (英语)](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-statsissocket-)
    -   [符号链接 ()](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-statsissymboliclink-)

## 类别 SFTPClient

SFTPClient 类提供了所有功能,可以与 SFTP服务员. 您可以使用密码或私钥连接到服务器 .

示例:使用密码从 sftp 服务器获取文件

```javascript
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 实例 .

**使用密码连接**

```javascript
await client.connect({
    hostname: 'l.dns.porn',
    port: 22,
    username: 'linaGirl',
    password: 'totally-secure',
});
```

**使用私钥连接**

```javascript
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 实例 .

**创建目录**

```javascript
await client.createDirectory('best-directory-ever');
```

**创建整个路径**

```javascript
await client.createDirectory('best-directory-ever/awesome-directory', true);
```

### SFTPClient.create. Read Stream (filePath) (中文(简体) )

使用可读流下载文件。 溪流的 API 与 标准 [节点.js 可读流](https://nodejs.org/dist/latest/docs/api/stream.html#stream_readable_streams)。这是您应当使用的方法 以下载文件到文件系统.

返回可读流.

**下载文件到文件系统**

```javascript
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 校验和**

```javascript
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 可写流](https://nodejs.org/dist/latest/docs/api/stream.html#stream_writable_streams)。 。 。 .

返回可写流 .

**从文件系统下载文件**

```javascript
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 实例 .

**删除目录**

```javascript
await client.deleteDirectory('best-directory-ever');
```

**递归删除目录**

```javascript
await client.deleteDirectory('best-directory-ever', true);
```

### SFTPClient.deleteFile (filePath) (英语)

删除一个文件.

返回 sftpClient 实例 .

**删除文件**

```javascript
await client.deleteFile('/path/to/file/that/needs/to.go');
```

### SFTPClient. end () (中文(简体) )

结束服务器的连接.

返回 sftpClient 实例 .

**结束连接**

```javascript
await client.end();
```

### SFTPClient. 存在(路径)

检查文件或目录是否存在.

返回布尔值 .

**检查文件是否存在**

```javascript
const fileExists = await client.exists('path/to/file');
```

**检查目录是否存在**

```javascript
const fileExists = await client.exists('path/to/directory');
```

### SFTPClient. 获得 文件 (文件路径)

将文件下载到缓冲器中.

返回包含所请求文件的缓冲器 .

**下载文件**

```javascript
const fileBuffer = await client.getFile('/path/to.file');
```

### SFTPClient.list (路径, 详细 = 虚假)

列出指定的路径。 路径可能是文件或目录.

返回包含文件名称的数组或包含对象的数组 详细文件信息.

**列出目录的文件**

```javascript
const filenames = await client.list('path/to/directory');

console.log(filenames);
// ['a.mjs', 'directory', 'z.jpg']
```

**列出目录文件, 返回细节**

```javascript
const files = await client.list('path/to/directory', true);

console.log(files);
[{filename: 'a.mjs', stats: StatsObject}]
```

见 [类数](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-stats) 对于目标的内容

### SFTPClient.move (源代码,目标代码)

将文件或目录从 `sourcePath` 页:1 `targetPath`。可用于 以重命名文件和目录.

返回 sftpClient 实例 .

**重命名文件**

```javascript
await client.move('nice-code.mjs', 'legacy-code.mjs');
```

**移动目录**

```javascript
await client.move('directory', 'some/path/for/the/directory');
```

### SFTPClient.put (英语). 文件( 文件 Path, 数据缓冲)

从缓冲器上传文件.

返回 sftpClient 实例 .

**上传文件**

```javascript
await client.putFile('/path/to.file', buffer);
```

### SFTPClient.set 许可(文件Path,权限)

在文件中设置权限。 如果您需要获得文件的权限 能够 [统计](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientstat-path) 这个!

返回 sftpClient 实例 .

**在文件中设置权限**

```javascript
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);
```

见 [类权限](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-permissions) 关于权限对象的细节.

### SFTPClient.stat (路径)

获取有关文件的数据。 报告文件的类型及其权限.

返回实例 [点级](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-stats)。 。 。 .

**获取文件数据**

```javascript
const stats = await client.stat('path/to/file');
```

见 [类数](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-stats) 用于统计对象的内容.

## 类权限

权限类保存关于文件系统权限的信息 对象。 它可用于获得对象的类型. 也可以用来读 或写入对象的权限.

权限对象作为属性返回 [数据](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-stats) 对象 [统计](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientstat-path) 编辑 [列表](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientlist-path-detailed--false) 打电话. 它可以传递给 [设置](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientsetpermissions-filepath-permissions) 调用更改对象的权限.

对象会显示权限属性 `user`,则 `group` 用于 `other` 每一个属性都是具有三种可写入属性的对象 显示 `read`, (中文(简体) ), `write` 财务报告和审定财务报表 `execute` 权限.

**给大家所有许可**

```javascript
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);
```

**检查用户在文件中是否有写入权限**

```javascript
const stats = await client.stat('path/to/file');

console.log(stats.permissions.user.write); // true or false
```

## 类数

数据对象由 [统计](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientstat-path) 编辑 [列表](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-sftpclientlist-path-detailed--false) 打电话.

对象的属性包含 [权限](https://npmcn.com/package/%40distributed-systems/sftp-client/#readme-class-permissions) 对象和一系列方法 用于确定 stat 调用路径的类型.

**使用统计方法获取数据**

```javascript
const stats = await client.stat('path/to/file');
```

### Stats.is BlockDevice () (英语)

回返 `true` 如果路径是一个块设备, `false` 否则.

**检查路径是否为块设备**

```javascript
const stats = await client.stat('path/to/file/or/directory');

console.log(stats.isBlockDevice()); // true or false
```

### Stats.is 沙拉克特·德维塞()

回返 `true` 如果路径是一个字符设备, `false` 否则.

**检查路径是否为字符设备**

```javascript
const stats = await client.stat('path/to/file/or/directory');

console.log(stats.isCharacterDevice()); // true or false
```

### Stats.is 导演( )

回返 `true` 如果路径是一个目录, `false` 否则.

**检查路径是否为目录**

```javascript
const stats = await client.stat('path/to/file/or/directory');

console.log(stats.isDirectory()); // true or false
```

### Stats.isFIFO () (中文(简体) )

回返 `true` 如果这条道是一条命名的管子, `false` 否则.

**检查路径是否是一个命名的管道**

```javascript
const stats = await client.stat('path/to/file/or/directory');

console.log(stats.isFIFO()); // true or false
```

### Stats.isFile () (中文(简体) )

回返 `true` 如果路径是普通文件, `false` 否则.

**检查路径是否为正则文件**

```javascript
const stats = await client.stat('path/to/file/or/directory');

console.log(stats.isFile()); // true or false
```

### 数据库(Socket)

回返 `true` 如果路径是插座, `false` 否则.

**检查路径是否为套接字**

```javascript
const stats = await client.stat('path/to/file/or/directory');

console.log(stats.isSocket()); // true or false
```

### Stats.is SymbolicLink () (中文(简体) )

回返 `true` 如果路径是象征性的链接, `false` 否则.

**检查路径是否是一个符号链接**

```javascript
const stats = await client.stat('path/to/file/or/directory');

console.log(stats.isSymbolicLink()); // true or false
```

## 常见问题

### @distributed-systems/sftp-client 是什么？

简单、充分测试和可靠的 SFTP 客户端

### 如何安装 @distributed-systems/sftp-client？

在项目目录运行 npm install @distributed-systems/sftp-client，或使用 pnpm add / yarn add / bun add。

### @distributed-systems/sftp-client 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @distributed-systems/sftp-client 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @distributed-systems/sftp-client 可以商用吗？

@distributed-systems/sftp-client 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @distributed-systems/sftp-client 的下载量有多少？

根据 npm 官方下载统计，@distributed-systems/sftp-client 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,173 次，日均约 139 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @distributed-systems/sftp-client 还在维护吗？

最新版本 2\.0\.0 发布于 2024/09/22（2 年前）；至今共发布 7 个版本，其中过去 12 个月发布了 0 个；首次发布于 2018/11/09。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

