使用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();
  });
相关推荐
Stardep4 天前
ssh远程连接服务器到vscode上“连接失败”
服务器·vscode·ssh
a.3024 天前
OpenCV(cv2)学习笔记:从模板匹配入门到常用函数
数据库·ubuntu·ssh
zz-zjx4 天前
堡垒机安全架构:从零搭建企业级防护(单节点版)
安全·ssh·安全架构
czhc11400756634 天前
LINUX913 shell:set ip [lindex $argv 0],\r,send_user,spawn ssh root@ip “cat “
tcp/ip·r语言·ssh
yyy0002006 天前
SSH登录管理
运维·服务器·ssh
临风.6 天前
使用Pycharm进行远程ssh(以Featurize为例)
ide·pycharm·ssh·featurize
彩虹海。8 天前
密码到期导致ssh连接失败
运维·ssh
胡萝卜的兔8 天前
ssh域名过期,消息推送到企业微信
运维·ssh·企业微信
软件测试很重要9 天前
UOS20系统安装与 SSH/XRDP 远程访问功能配置指南
linux·运维·ssh
qinyia10 天前
用 Wisdom SSH 轻松实现服务器自动化任务调度
服务器·自动化·ssh