node.js - 如何使用basic-ftp模块连接到节点中的FTPS服务器
问题描述
我正在使用https://www.npmjs.com/package/basic-ftp基本 ftp 包连接到 ftps 服务器。我已经尝试过我的其他扩展,但无法连接到 ftps 服务器 下面是我的代码
const ftp = require("basic-ftp")
example();
async function example() {
const client = new ftp.Client()
client.ftp.verbose = true
try {
await client.access({
host: "ftp.xxxx.xxxxx",
user: "xxxx@xxxx.xx",
password: "xxxxxxx",
secure :true
})
await client.ensureDir("/my/remote/directory")
console.log(await client.list())
await client.uploadFrom("temp/readme.txt", "readme.txt")
// await client.downloadTo("README_COPY.md", "README_FTP.md")
}
catch(err) {
console.log(err)
}
client.close()
}
但给我一个错误
Connected to xxx.xxx.xx.xxx:21
< 220 Service ready for new user.
Login security: No encryption
> USER xx@xxx.xx
< 331 User name okay, need password for xxxx@xxx.xx.
> PASS ###
< 530 Box: Smartest Energy does not allow regular FTP; use FTPS instead. (Both "
explicit" and "implicit" FTPS are supported.)
{ FTPError: 530 Box: Smartest Energy does not allow regular FTP; use FTPS instea
d. (Both "explicit" and "implicit" FTPS are supported.)
at FTPContext._onControlSocketData (D:\node\basicftp\node_modules\basic-ftp\
dist\FtpContext.js:276:39)
at Socket.socket.on.data (D:\node\basicftp\node_modules\basic-ftp\dist\FtpCo
ntext.js:121:44)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:265:13)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) name
: 'FTPError', code: 530 }
请帮助提前谢谢
解决方案
您将需要连接Explicit FTPS over TLS
。要通过 tls 连接到 ftps,您需要传递以下选项:
const fs = require('fs');
async function example() {
const client = new ftp.Client()
client.ftp.verbose = true
try {
const secureOptions = {
// Necessary only if the server requires client certificate authentication.
key: fs.readFileSync('client-key.pem'),
cert: fs.readFileSync('client-cert.pem'),
// Necessary only if the server uses a self-signed certificate.
ca: [ fs.readFileSync('server-cert.pem') ],
// Necessary only if the server's cert isn't for "localhost".
checkServerIdentity: () => { return null; },
};
await client.access({
host: "ftp.xxxx.xxxxx",
user: "xxxx@xxxx.xx",
password: "xxxxxxx",
secure :true,
secureOptions : secureOptions
})
await client.ensureDir("/my/remote/directory")
console.log(await client.list())
await client.uploadFrom("temp/readme.txt", "readme.txt")
// await client.downloadTo("README_COPY.md", "README_FTP.md")
}
catch(err) {
console.log(err)
}
client.close()
}
推荐阅读
- windows - 用于在同一字典中查找重复文件名的批处理脚本
- vue.js - 如何将字体和颜色变量(在页面加载时从后端 api 获取)注入 Nuxt.js 样式?
- android - 动态结束 Flutter ListView
- powerbi - DAX 计数最大值(按组)
- python - 满足条件时更改 PyQt5 QTableWidget 行样式表
- delphi - 在 Delphi 10.3.3 中使用 RTTI 收集一个类的所有后代
- javascript - 如何通过按一个键或最好是 2 个键在 Chrome/Firefox 上启动网站
- javascript - 用于检查重复的正则表达式
- vba - 代码运行正常,但单步执行时出错
- javascript - Google Sheets + Monday.com API - 更改列值