# FTP/SFTP Client

> 通过 FTP/SFTP 传输文件。

Source: https://docs.sommatic.ai/zh/nodes/io.ftp/

- 标识: `io.ftp`
- 类别: 输入与输出
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

通过 FTP/SFTP 传输文件。

## 配置

- `connection_id` (string, 必填): FTP/SFTP 服务器的凭据。接受 `basic-auth`（用户名 + 密码 + 主机 + 端口）或 `connection-string`（ftp:// / sftp:// URI）类型的凭据。
- `operation` (string, 必填): 默认值: `list`. 可选值: `list`, `upload`, `download`, `delete`.
- `remotePath` (string, 可选)
- `localPath` (string, 可选): 用于上传/下载
- `fileContent` (string, 可选): 用于上传的直接内容

## 输出端口

- `out` (默认): 操作结果
- `error`: 传输失败

## 输入契约

- `connection_id` (string, 必填)
- `operation` (string, 必填)
- `remotePath` (string, 可选)
- `localPath` (string, 可选)
- `fileContent` (string, 可选)

## 输出契约

- `result` (object, 可选): FTP 操作结果（例如列表或成功状态）

## 在表达式中

后续节点可通过 `$("FTP/SFTP Client").item.json.<路径>` 读取此节点输出中的内容（请把 `FTP/SFTP Client` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("FTP/SFTP Client").item.json.result` (object) = `[…] (2)`: FTP 操作结果（例如列表或成功状态）


## 工作原理

通过 FTP、FTPS 或 SFTP 列出、上传、下载或删除单个远程文件，凭据在服务器端解析。

1. 解析配置。`connection_id`、`operation`（默认 `list`）、`remotePath` 和 `fileContent` 缺失时回退到输入 payload 中的同名键。
2. 解析凭据：`host`（默认 `127.0.0.1`）、`port`、`protocol` 和 `secure` 取自凭据配置；`username` 和 `password` 取自凭据密钥。
3. 选择协议：端口为 22 或 2222、或 `protocol` 为 `sftp` 时使用 SFTP；否则使用 FTP（`secure` 为 true 时使用 FTPS）。凭据未设置端口时，SFTP 使用 22，FTP 使用 21。
4. 执行操作：`list`（列出 `remotePath` 目录）、`upload`（将 `fileContent` 写入 `remotePath`）、`download`（以 UTF-8 文本读取 `remotePath`）或 `delete`。
5. 从 `out` 返回 `{ result }`，关闭连接，并将协议错误发送到 `error`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-download-orders",
  "label": "Download orders file",
  "operator_slug": "io.ftp",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "operation": "download",
    "remotePath": "/outbound/orders-{{ $json.date }}.csv"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": {
      "content": "order_id,amount\nord_123,250.00\n"
    }
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

操作成功。`list` 时 `result` 为条目数组，`download` 时为 `{ content }`，`upload` 时为 `{ uploaded: true, path }`，`delete` 时为 `{ deleted: true }`。FTP 列表条目包含库提供的文件信息（`name`、`type`：1 = 文件，2 = 目录、`size`、`rawModifiedAt` 等）；SFTP 条目仅含 `name`、`type` 和 `size`。

```json
{
  "result": [
    {
      "name": "orders-2026-11-25.csv",
      "type": 1,
      "size": 18342
    },
    {
      "name": "archive",
      "type": 2,
      "size": 4096
    }
  ]
}
```

### `error`

登录、传输或路径错误，操作缺少参数，不支持的操作，或 SFTP 连接提前关闭。

```json
{
  "error": "remotePath is required for download"
}
```

## 代码中的行为

- `connection_id`: 接受 `basic-auth` 凭据：其配置包含 `host`、`port`、`protocol`（设为 `sftp` 可强制使用 SFTP）和 `secure`，密钥包含 `username` 和 `password`。`connection-string` 类型的密钥（`ftp://` URI）不会被解析，主机和端口必须写在凭据配置中。以 `{` 开头的值会被解析为内联 JSON `{ config, credentials }`，而非凭据 id；仅限本地测试使用。
- `localPath`: 已声明但被忽略：上传读取 `fileContent`，下载在输出中返回内容，不会读写本地磁盘。
- `remotePath`: 为空时，`list` 使用 `/`（FTP）或 `.`（SFTP）。
- `port`: 未声明，仅作提示读取：凭据未设置端口且节点配置为 `port: 22` 时，使用端口 22 的 SFTP。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `connection_id is required` | `error` | 在连接前抛出，因此引擎会将该节点判定为失败（若无 `flow.try` 作用域捕获，整个运行也会失败）。请选择 FTP 或 SFTP 凭据。 |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | 凭据不存在或此工作流无权使用。同样会抛出异常，因此在 `flow.try` 作用域之外运行会失败。 |
| `fileContent is required for upload` | `error` | FTP `upload` 缺少内容（路径缺失时 FTP 对应报 `remotePath is required for upload` 和 `remotePath is required for download`；SFTP 报 `fileContent is required`）。 |
| `Unsupported operation: <operation>` | `error` | `operation` 不是 `list`、`upload`、`download` 或 `delete`。 |
| `SSH connection closed` | `error` | SFTP 服务器在操作完成前关闭了连接，通常是因为登录被拒绝。 |

## 说明与限制

- 下载内容以 UTF-8 文本返回，二进制文件会损坏。上传时 `fileContent` 以文本发送。
- SFTP 握手最多等待 15 秒。FTP 除库默认值外没有显式超时。不会重试。
- 凭据密钥在运行时于服务器端解析，不会出现在输出中。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "io.ftp",
  "name": "FTP/SFTP Client",
  "category": "io",
  "version": "1.0.0",
  "description": "Transfer files via FTP/SFTP.",
  "capabilities": [
    "storage",
    "ftp",
    "sftp"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "FTP / SFTP Credential",
        "description": "Credential for the FTP/SFTP server. Accepts credentials of type `basic-auth` (username + password + host + port) or `connection-string` (ftp:// / sftp:// URI).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "basic-auth",
            "connection-string"
          ],
          "types": [
            "ftp",
            "sftp"
          ]
        }
      },
      "operation": {
        "type": "string",
        "enum": [
          "list",
          "upload",
          "download",
          "delete"
        ],
        "default": "list"
      },
      "remotePath": {
        "type": "string"
      },
      "localPath": {
        "type": "string",
        "description": "For upload/download"
      },
      "fileContent": {
        "type": "string",
        "description": "Direct content for upload"
      }
    },
    "required": [
      "connection_id",
      "operation"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "operation": {
        "type": "string"
      },
      "remotePath": {
        "type": "string"
      },
      "localPath": {
        "type": "string"
      },
      "fileContent": {
        "type": "string"
      }
    },
    "required": [
      "connection_id",
      "operation"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "object",
        "description": "FTP operation result (e.g. list, or success)"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Result",
      "description": "Operation result",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Transfer failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io"
  ],
  "tags": [
    "storage",
    "legacy"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "FTP / SFTP Credential",
      "description": "Credential for the FTP/SFTP server. Accepts credentials of type `basic-auth` (username + password + host + port) or `connection-string` (ftp:// / sftp:// URI).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "basic-auth",
          "connection-string"
        ],
        "types": [
          "ftp",
          "sftp"
        ]
      }
    },
    "operation": {
      "type": "string",
      "enum": [
        "list",
        "upload",
        "download",
        "delete"
      ],
      "default": "list"
    },
    "remotePath": {
      "type": "string"
    },
    "localPath": {
      "type": "string",
      "description": "For upload/download"
    },
    "fileContent": {
      "type": "string",
      "description": "Direct content for upload"
    }
  },
  "required": [
    "connection_id",
    "operation"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "operation": {
      "type": "string"
    },
    "remotePath": {
      "type": "string"
    },
    "localPath": {
      "type": "string"
    },
    "fileContent": {
      "type": "string"
    }
  },
  "required": [
    "connection_id",
    "operation"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "object",
      "description": "FTP operation result (e.g. list, or success)"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Result",
    "description": "Operation result",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Transfer failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
