使用ssh上传数据到阿里云ESC云服务上

在这之前需要安装 ssh2-sftp-client 直接在终端输入:npm i ssh2-sftp-client

直接上代码:

javascript 复制代码
const path = require('path');
const Client = require('ssh2-sftp-client');

// 配置连接参数
const config = {
  host: 'your-server-ip', // 云服务器的IP地址
  port: 22, // SSH端口号,默认为22
  username: 'your-username', // SSH用户名
  password: 'your-password' // SSH密码
};

// 创建 SFTP 客户端
const sftp = new Client();

// 上传的本地文件路径
const localFilePath = path.resolve(__dirname, 'userInfo.json'); // 我这里数据userInfo.json

// 远程服务器上的目标路径
const remoteFilePath = '/root/userInfo.json';    // 我这里的目录是/root
// 后面只是上传数据的名字,如果没有弄后面的userInfo.json,上传默认数据名字就是root

sftp.connect(config)
  .then(() => {
    return sftp.put(localFilePath, remoteFilePath);
  })
  .then(() => {
    console.log(`JSON 文件上传成功`);
    sftp.end();
  })
  .catch((err) => {
    console.error(`上传文件时出错: ${err.message}`);
    sftp.end();
  });
相关推荐
REDcker12 小时前
GitHub SSH 连接失败排障实录
运维·ssh·github
洪恒远1 天前
Windows 配置 Gerrit SSH Key
运维·ssh
Web3探索者11 天前
可视化服务器管理和传统命令行区别是什么?新手教程:Linux 运维到底该用图形界面还是 SSH 命令行?
linux·ssh
开发者联盟league18 天前
安装pnpm
ssh
2601_9618752418 天前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant
小小小花儿19 天前
SSH密钥配置(免密连接远程服务器)
服务器·ssh
m0_5261194019 天前
ssh key生成,gitee配置ssh
运维·gitee·ssh
qq_3384323719 天前
VSCode Remote-SSH 远程 Windows Server 卡死的排查与解决
windows·vscode·ssh
着迷不白19 天前
十、网络客户端工具curl, wget, ssh, scp, sftp, rsync
运维·网络·ssh
江畔柳前堤20 天前
github实战指南01-账号配置与 SSH 密钥
运维·人工智能·深度学习·ssh·github·pyqt·信号处理