使用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();
  });
相关推荐
Lois_Luo10 小时前
使用 DigitalPlat 免费搭配 Cloudflare Tunnel 实现飞牛系统、服务及 SSH 内网穿透教程
ssh·内网穿透
超龄超能程序猿13 小时前
Bitvisse SSH Client 安装配置文档
运维·ssh·github
电脑能手2 天前
遇到该问题:kex_exchange_identification: read: Connection reset`的解决办法
linux·ubuntu·ssh
half-beast12 天前
ganymed-ssh2连接openssh 8.2
java·ssh·openssh·ganymed
qq_2148032912 天前
systemd[1]: Failed to start LSB: Bring up/down networking
ssh
家庭云计算专家12 天前
群晖如何开启及使用ssh:小白用户上手指南-家庭云计算专家
运维·云计算·ssh·onlyoffice
SH11HF13 天前
小菜狗的云计算之旅,shell脚本语言的基本内容和用法
运维·网络·云计算·ssh
KIKU啊13 天前
Docker+PyCharm+ssh部署深度学习docker环境
docker·pycharm·ssh
问~14 天前
gitea本地部署代码托管后仓库的新建与使用(配置好ssh密钥后仍然无法正常克隆仓库是什么原因)
运维·ssh·gitea
fangeqin14 天前
CentOS 8解决ssh连接github时sign_and_send_pubkey失败问题
centos·ssh·github