sftp sync extension for VS CodeVS Code的sftp同步扩展

Syncs your local directory with a remote server directory. 将本地目录与远程服务器目录同步。Allows you to optionally edit upload a file to the remote directory after it saves locally. 允许您在本地保存文件后,选择性地编辑将文件上载到远程目录。This allows you to edit more or less directly on the server similar to WinScp or other similar programs.这使您可以或多或少地直接在服务器上编辑,类似于WinScp或其他类似程序。

Very powerful, with smart features. Very simple, requires just three lines of config! Very fast, finished in a blink.动力很强,功能也很智能。非常简单,只需要三行配置!速度很快,一眨眼就完成了。

Usage用法

If your latest files are on the server, you can start with an empty local folder, then download your project, and from that point sync.如果你的最新文件在服务器上,你可以从一个空的本地文件夹开始,然后下载你的项目,并从那时开始同步。

  1. In VS Code, open a local directory you wish to sync to the remote server (or create an empty directory that you wish to first download the contents of a remote server folder in order to edit locally).在VS Code中,打开一个要同步到远程服务器的本地目录(或创建一个空目录,以便首先下载远程服务器文件夹的内容以便在本地编辑)。
  2. Ctrl+Shift+P on Windows/Linux or Cmd+Shift+P on Mac open command palette, run SFTP: config command.Windows/Linux上的Ctrl+Shift+P或Mac上的Cmd+Shift+P打开命令面板,运行SFTP:config命令。
  3. A basic configuration file will appear named sftp.json under the .vscode directory. 一个名为sftp.json的基本配置文件将出现在.vscode目录下。Edit the parameters to match your setup.编辑参数以匹配您的设置。

For instance:例如:

{
    "name": "Profile Name",
    "host": "name_of_remote_host",
    "protocol": "ftp",
    "port": 21,
    "secure": true,
    "username": "username",
    "remotePath": "/public_html/project",  <---- This is the path which will be downloaded if you "Download Project"
    "password": "password",  
    "uploadOnSave": true
}

The password parameter is optional but if you don't add it, you will be constantly prompted for a password. password参数是可选的,但如果不添加,系统会不断提示您输入密码。Note that you must escape any backslashes and other special characters with a backslash.请注意,必须用反斜杠转义任何反斜杠和其他特殊字符。

There are other Example Configs below.下面还有其他示例配置。

  1. Save and close the sftp.json file.保存并关闭sftp.json文件。
  2. Ctrl+Shift+P on Windows/Linux or Cmd+Shift+P on Mac open command palette.Windows/Linux上的Ctrl+Shift+P或Mac上的Cmd+Shift+P打开命令调色板。
  3. Type "sftp" and you'll now see a number of other comands.键入“sftp”,您现在将看到许多其他命令。
  4. A good one to start with if you want to start with a remote folder is SFTP: Download Project. 如果你想从远程文件夹开始,一个很好的开始是SFTP: Download ProjectThis will download the directory shown in the remotePath setting in sftp.json to your local open directory.这将把sftp.jsonremotePath设置中显示的目录下载到您的本地打开目录。
  5. Done搞定了 - you can now edit locally and after each save it will upload to your remote file.您现在可以在本地编辑,每次保存后都会上传到您的远程文件。
  6. Enjoy !享受它吧!

For detailed usage. 详细用法。Please go to wiki.请访问wiki

Example Configs配置示例

You can see the full config here.你可以在这里看到完整的配置。

Simple易于理解的

{
  "host": "host",
  "username": "username",
  "remotePath": "/remote/workspace"
}

Profiles配置文件

{
  "username": "username",
  "password": "password",
  "remotePath": "/remote/workspace/a",
  "watcher": {
    "files": "dist/*.{js,css}",
    "autoUpload": false,
    "autoDelete": false
  },
  "profiles": {
    "dev": {
      "host": "dev-host",
      "remotePath": "/dev",
      "uploadOnSave": true
    },
    "prod": {
      "host": "prod-host",
      "remotePath": "/prod"
    }
  },
  "defaultProfile": "dev"
}

Note: context and watcher are only available at root level.仅在根级别可用。

Use SFTP: Set Profile to switch profile.使用SFTP: Set Profile以切换配置文件。

Multiple Context多个上下文

The context must not be same.上下文不能相同

[
  {
    "name": "server1",
    "context": "project/build",
    "host": "host",
    "username": "username",
    "password": "password",
    "remotePath": "/remote/project/build"
  },
  {
    "name": "server2",
    "context": "project/src",
    "host": "host",
    "username": "username",
    "password": "password",
    "remotePath": "/remote/project/src"
  }
]

Note: name is required in this mode.此模式下需要name

Connection Hopping连接跳跃

You can connect to a target server through a proxy with ssh protocol.您可以通过使用ssh协议的代理连接到目标服务器。

Note: Variable substitution is not working in a hop config.变量替换在跃点配置中不起作用。

Single Hop单跳

local -> hop -> target

{
  "name": "target",
  "remotePath": "/path/in/target",
  
  // hop
  "host": "hopHost",
  "username": "hopUsername",
  "privateKeyPath": "/Users/localUser/.ssh/id_rsa", // The key file is assumed on the local.

  "hop": {
    // target
    "host": "targetHost",
    "username": "targetUsername",
    "privateKeyPath": "/Users/hopUser/.ssh/id_rsa", // The key file is assumed on the hop.
  }
}

Multiple Hop多跳

local -> hopa -> hopb -> target

{
  "name": "target",
  "remotePath": "/path/in/target",

  // hopa
  "host": "hopAHost",
  "username": "hopAUsername",
  "privateKeyPath": "/Users/hopAUsername/.ssh/id_rsa" // The key file is assumed on the local.

  "hop": [
    // hopb
    {
      "host": "hopBHost",
      "username": "hopBUsername",
      "privateKeyPath": "/Users/hopaUser/.ssh/id_rsa" // The key file is assumed on the hopa.
    },

    // target
    {
      "host": "targetHost",
      "username": "targetUsername",
      "privateKeyPath": "/Users/hopbUser/.ssh/id_rsa", // The key file is assumed on the hopb.
    }
  ]
}

Config in User Setting用户设置中的配置

You can use remote to tell sftp to get the config from remote-fs.您可以使用remote告诉sftp从远程fs获取配置。

In User Setting:在用户设置中:

"remotefs.remote": {
  "dev": {
    "scheme": "sftp",
    "host": "host",
    "username": "username",
    "rootPath": "/path/to/somewhere"
  },
  "projectX": {
    "scheme": "sftp",
    "host": "host",
    "username": "username",
    "privateKeyPath": "/Users/xx/.ssh/id_rsa",
    "rootPath": "/home/foo/some/projectx"
  }
}

In sftp.json:sftp.json中:

{
  "remote": "dev",
  "remotePath": "/home/xx/",
  "uploadOnSave": true,
  "ignore": [".vscode", ".git", ".DS_Store"]
}

Remote Explorer远程资源管理器

remote-explorer-preview

Remote Explorer lets you explore files in remote. 远程资源管理器允许您以远程方式浏览文件。You can open Remote Explorer by:您可以通过以下方式打开远程资源管理器:

  1. Run Command View: Show SFTP.运行命令View: Show SFTP
  2. Click SFTP view in Activity Bar.单击活动栏中的SFTP视图。

You can only view a files content with Remote Explorer. 您只能使用远程资源管理器查看文件内容。Run command SFTP: Edit in Local to edit it in local.运行命令SFTP: Edit in Local以在本地编辑它。

Note: You need to manually refresh the parent folder after you delete a file to make the explorer updated.删除文件后,您需要手动刷新父文件夹以更新资源管理器。

Debug调试

  1. Open User Settings.打开“用户设置”。

    • On Windows/Linux - File > Preferences > Settings
    • On macOS - Code > Preferences > Settings
  2. Set sftp.debug to true and reload vscode.

  3. View the logs in View > Output > sftp.