使用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();
  });
相关推荐
dyxal4 小时前
内网windows系统机器通过vscode ssh连接内网服务器,无网络导致的一系列无法正常连接问题
服务器·vscode·ssh
qq_3391911419 小时前
aws ec2防ssh爆破, aws服务器加固, 亚马逊服务器ssh安全,防止ip扫描ssh。 aws安装fail2ban, ec2配置fail2ban
服务器·ssh·aws
北城笑笑1 天前
Git 10 ,使用 SSH 提升 Git 操作速度实践指南( Git 拉取推送响应慢 )
前端·git·ssh
蓁蓁啊1 天前
GIT使用SSH 多账户配置
运维·git·ssh
等风来不如迎风去2 天前
用你本地已有的私钥(private key)去 SSH 登录远程 Ubuntu 服务器
服务器·ubuntu·ssh
万山y2 天前
WSL SSH 服务器一站式配置教程
运维·服务器·ssh
刘某的Cloud3 天前
SSH命令建立隧道
linux·运维·ssh·系统·shell
A-刘晨阳4 天前
【Linux】Centos 8 默认OpenSSH 升级OpenSSH9.8【升级其他OpenSSH版本通用】
linux·运维·centos·ssh·openssh·centos8
漂亮_大男孩4 天前
Git|GitHub SSH 连接配置与验证全流程(通用方法)
git·ssh·github
饭来_6 天前
通过 SSH 远程连接 docker 容器
vscode·docker·ssh