使用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();
  });
相关推荐
qinyia15 小时前
用 Wisdom SSH 轻松实现服务器自动化任务调度
服务器·自动化·ssh
qinyia1 天前
Wisdom SSH 是一款创新性工具,通过集成 AI 助手,为服务器性能优化带来极大便利。
服务器·人工智能·ssh
JCGKS1 天前
Docker|“ssh: connect to host xxx.xxx.xxx.xxx port 8000: Connection refused“问题解决
docker·ssh·端口·listen·tcp三次握手
fengfuyao9851 天前
诊断并修复SSH连接Github时遇到的“connection closed“错误
运维·ssh·github
高旭的旭1 天前
Gitlab 配置自定义 clone 地址
svn·ssh·gitlab·code
小猫挖掘机(绝版)2 天前
通过tailscale实现一台电脑上vscode通过ssh连接另一台电脑上的VMware Linux 虚拟机
linux·windows·vscode·ubuntu·ssh
方先森有点懒2 天前
CentOS 7.2 虚机 ssh 登录报错在重启后无法进入系统
linux·运维·centos·ssh
喜欢你,还有大家2 天前
SSH服务远程安全登录
运维·安全·ssh
qinyia2 天前
解锁服务器网络配置新姿势:Wisdom SSH 助力之旅
服务器·网络·ssh
三无少女指南3 天前
WSL2环境下因服务器重装引发的SSH连接问题排查记录
运维·服务器·ssh