You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core AWS Lambda函数中WinSCP.exe缺失,如何指定其路径?

在AWS Lambda中设置WinSCP.exe路径的解决方案

针对你遇到的问题,可通过以下步骤解决:

1. 确保WinSCP可执行文件被正确部署

首先在项目文件(.csproj)中添加配置,确保WinSCP的可执行文件被复制到输出目录,避免部署Lambda时遗漏:

<ItemGroup>
  <!-- 对应你的Lambda运行时架构,如win-x64或linux-x64 -->
  <Content Include="win-x64\WinSCP.exe">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
  <Content Include="win-x64\WinSCP.com">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

2. 获取Lambda中的可执行文件路径

AWS Lambda中可通过两种方式获取程序运行的基目录,再拼接WinSCP.exe的路径:

方式一:通过程序集路径获取

using System.Reflection;
using WinSCP;

public async Task FunctionHandler(ILambdaContext context)
{
    // 获取当前程序集所在目录
    var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    // 拼接WinSCP.exe的完整路径(根据你的运行时目录调整)
    var winscpExecutablePath = Path.Combine(assemblyDirectory, "win-x64", "WinSCP.exe");

    var sessionOptions = new SessionOptions
    {
        // 配置SFTP连接参数
        Protocol = Protocol.Sftp,
        HostName = "你的SFTP服务器地址",
        UserName = "用户名",
        Password = "密码",
        SshHostKeyFingerprint = "服务器SSH指纹"
    };

    using var session = new Session();
    session.ExecutablePath = winscpExecutablePath;
    session.Open(sessionOptions);
    
    // 执行文件上传等操作
    // ...
}

方式二:通过应用程序基目录获取

使用AppContext.BaseDirectory直接获取应用运行的基目录,代码更简洁:

using WinSCP;

public async Task FunctionHandler(ILambdaContext context)
{
    var winscpExecutablePath = Path.Combine(AppContext.BaseDirectory, "win-x64", "WinSCP.exe");
    
    // 后续连接逻辑同上
}

3. 注意事项

  • 匹配Lambda运行时:如果使用Linux环境的Lambda,需替换为linux-x64目录下的winscp可执行文件(无.exe后缀)
  • 验证部署包:在Lambda控制台的代码包内容中确认WinSCP的文件是否存在,避免部署时遗漏
  • 部署工具适配:若使用Serverless Framework或SAM部署,需在配置中添加文件包含规则,确保WinSCP文件被打包

内容的提问来源于stack exchange,提问作者henrik55

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 02:25:10