使用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();
  });
相关推荐
PLUS_WAVE8 小时前
【SSH 端口转发】通过SSH端口转发实现访问远程服务器的 tensorboard
linux·服务器·ssh·软件工程·端口转发·tensorboard
极小狐9 小时前
极狐GitLab 的压缩和合并是什么?
运维·git·ssh·gitlab·github
极小狐16 小时前
极狐GitLab 合并请求依赖如何解决?
运维·git·ssh·gitlab·github
Cloud_Air75419 小时前
从零开始使用SSH链接目标主机(包括Github添加SSH验证,主机连接远程机SSH验证)
运维·ssh
宁酱醇2 天前
GitLab_密钥生成(SSH-key)
运维·ssh·gitlab
GnixAij2 天前
Docker SSH端口转发
docker·ssh
明·煜2 天前
解决VSCode每次SSH连接服务器时,都需要下载vscode-server
ide·vscode·ssh
深夜情感老师3 天前
centos离线安装ssh
linux·centos·ssh
我的golang之路果然有问题3 天前
给git配置SSH(github,gitee)
经验分享·笔记·git·学习·gitee·ssh·github
极小狐3 天前
极狐GitLab Git LFS 速率限制如何设置?
运维·git·ssh·gitlab·github