使用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();
  });
相关推荐
没有不重的名么1 天前
Tmux Xftp及Xshell的服务器使用方法
服务器·人工智能·深度学习·机器学习·ssh
妮妮喔妮4 天前
SSH协议的GIT转换
运维·git·ssh
conkl5 天前
Linux 零基础万字入门指南(进阶详解版)
linux·运维·服务器·ssh·文件管理·shell·linux基础
Tearstornbyrain5 天前
在Ubuntu24.04中使用ssh连接本地git仓库到github远程仓库
linux·git·ubuntu·ssh·github
CodeDevMaster8 天前
Linux中tmux入门使用指南:告别SSH断线烦恼,提升终端工作效率的神器
linux·运维·ssh
monster_风铃10 天前
华为实验:SSH
运维·ssh
胖大和尚14 天前
ssh 公钥连接服务器失败,排查方向
运维·服务器·ssh
qinyia14 天前
Wisdom SSH:自动化网络配置管理的领航者
网络·自动化·ssh
whabc10015 天前
ssh连接VirtualBox中的Ubuntu24.04(win11、putty、NAT 模式)
运维·ssh