Visual Studio Code SFTP 连接多个服务器

9
在PhpStorm中,有一种方法可以配置多个SFTP终端并选择要上传到哪个服务器。我正在寻找Visual Studio Code中的此功能。我已安装SFTP VS Code扩展程序,并且能够为一个终端进行配置。如果我想将文件上传到多个服务器怎么办?我该如何配置?或者是否有另一个扩展程序可以实现这一点?

你试过这个扩展吗?https://marketplace.visualstudio.com/items?itemName=liximomo.sftp#multiple-context - Charlie Parker
相关问题:https://github.com/liximomo/vscode-sftp/issues/746 - Charlie Parker
6个回答

21

你可以在配置中添加多个FTP服务器。只需确保context不同即可。

[
  {
    "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"
  }
]


1
澄清一下,“上下文”是指文件将被下载到的路径,并且为了避免与不同文件夹中具有相同名称的文件夹/文件发生冲突。此外,示例将它们显示为绝对路径,从根文件夹/开始。我建议将它们设置为相对于您的默认SFTP文件夹,例如./path/to/server1files - DiegoDD
2
请注意,您无法在相同的上下文中使用多个服务器。 - Senju
这个选项是让我一次上传到一个服务器,还是会同时同步到两个服务器? - A. Adam
@DiegoDD 你是指“从哪里下载路径”还是“从哪里上传路径”? - Charlie Parker
当我尝试使用你的解决方案时,只有一个服务器更新了。为什么? - Charlie Parker
显示剩余3条评论

14

现在您可以使用SFTP扩展中的“配置文件”。https://github.com/liximomo/vscode-sftp#profiles

{
  "name": "My Project",
  "protocol": "sftp",
  "remotePath": "/",
  "port": 22,
  "profiles": {
    "dev": {
      "host": "server1.example.com",
      "username": "username",
      "password": "password"
    },
    "prod": {
      "host": "server2.example.com",
      "username": "other-username",
      "password": "other-password"
    }
  },
  "defaultProfile": "dev"
}

你能否拥有单独的远程路径而不是在项目顶部只有一个吗? - Charlie Parker
1
另外,我想在保存时同时推送到两个地方,而不仅仅是其中一个,这可能吗? - Charlie Parker

6
你可以为每个配置文件设置远程路径,如下所示:(我看到@Charlie Parker在那个问题的第二个答案中问过这样的问题)
{
    "name": "ExampleName",
    "protocol": "sftp",
    "port": 22,
    "profiles": {
        "profile1": {
            "host": "connection1",
            "username": "user1",
            "remotePath":"/path1"
        },
        "profile2": {
            "host": "connection2",
            "username": "user2",
            "remotePath":"/path2"
        },
        "profile3": {
            "host": "connection3",
            "username": "user3",
            "remotePath":"/path3"
        },
    }
}

通过按下 Ctrl+Shift+P > Set Profile,您可以更改您的个人资料。


2

为什么这个回答被接受了,当 OP 明显要求来自 liximomo(现在是 Natizyskunk)的 sFTP 扩展?该扩展可以轻松处理多个配置文件,无需添加其他扩展或更改它。 - Warface

2

试试这个:

    [
    {
      "name": "project 1",
      "context": "/project/project1",
      "host": "",
      "username": "",
      "password": "",
      "protocol": "ftp",
      "post": 21,
      "remotePath": "/",
      "uploadOnSave": true 
    },
    {
      "name": "project 2",
      "context": "/project/project2",
      "host": "",
      "username": "",
      "password": "",
      "protocol": "ftp",
      "post": 21,
      "remotePath": "/",
      "uploadOnSave": true 
    }
  ]

1

[
  {
    "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"
  }
]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接