【问题标题】:Issues moving file with WinSCP in a WebJob在 WebJob 中使用 WinSCP 移动文件时出现问题
【发布时间】:2019-06-04 14:33:32
【问题描述】:

我在网络作业中将文件从存储库移动到另一个存储库时遇到了一些问题。 我使用命令_session.MoveFile("mypathsource","mypathdestination")。 我的网络作业每 10 分钟触发一次。

当我部署我的网络作业时,它会移动文件,但在我的网络作业迭代几次后,它不会移动文件。它没有抛出任何异常。它只是不移动文件。

我已将 NuGet 包 WinSCP 更新到版本 5.15.2。我使用 .Net 框架 4.6.1。

public void SendFileToArchive(string fileName)
{
    _log.DebugFormat("Deleting file on local path.");
    File.Delete($"{_config.LocalPath}\\{fileName}");
    if (!_session.Opened)
    {
       _log.DebugFormat("Session is closed -> Open it.");
        this.OpenSession();
    }
    _log.DebugFormat("Move file.");
    _session.MoveFile($"{fileName}", "Archive/" + fileName);
    if (_session.FileExists(fileName))
    {
        _log.Error("File hasn't be moved");
    }
 }

private void OpenSession()
{
   // Configurer les options de session
   SessionOptions sessionOptions = new SessionOptions
   {
       Protocol = Protocol.Sftp,
       HostName = _config.FtpUrl,
       UserName = _config.FtpLogin,
       Password = _config.FtpPwd,
       SshHostKeyFingerprint = _config.SshHostKeyFingerprint,
    };
    sessionOptions.AddRawSettings("ProxyPort", "0");
    _session = new Session();
    // Connecter
    _session.Open(sessionOptions);
}

我希望文件被移动,或者至少该进程会引发异常。 有谁知道我做错了什么?

我可以在移动它之前尝试检查目标文件是否存在,但在我的用例中,如果这是原因,我会非常惊讶。因为我在出错后手动检查并且文件只存在于源路径中。

对于 openSession,我没有向您发送所有代码,但我在构造函数中调用该方法(在我的 webjob 中仅使用一次)或者当我在 move 方法中检查会话是否关闭时(我添加了该部分当我看到移动部分不起作用时检查连接的状态。

当然,当我完成所有过程时,我会调用一个 dispose 方法:

public void DisposeSession()
{
    if (this._session.Opened)
    {
        this._session.Close();
    }

    this._session.Dispose();
}

这是日志:

. 2019-06-05 15:21:21.420 Listing file "FILETOMOVE_20190524_010139.json".
> 2019-06-05 15:21:21.420 Type: SSH_FXP_LSTAT, Size: 44, Number: 5639
< 2019-06-05 15:21:21.420 Type: SSH_FXP_STATUS, Size: 26, Number: 5380
. 2019-06-05 15:21:21.420 Discarding reserved response
< 2019-06-05 15:21:21.420 Type: SSH_FXP_ATTRS, Size: 54, Number: 5639
. 2019-06-05 15:21:21.420 FILETOMOVE_20190524_010139.json;-;1203;2019-05-23T23:01:40.000Z;3;"root" [0];"root" [0];rwxrwxrwx;1
< 2019-06-05 15:21:21.420 Script: -rwxrwxrwx   0 root     root          1203 May 23 23:01:40 2019 FILETOMOVE_20190524_010139.json
> 2019-06-05 15:21:21.482 Script: stat -- "Archive/FILETOMOVE_20190524_010139.json"
. 2019-06-05 15:21:21.482 Listing file "Archive/FILETOMOVE_20190524_010139.json".
> 2019-06-05 15:21:21.482 Type: SSH_FXP_LSTAT, Size: 52, Number: 5895
< 2019-06-05 15:21:21.482 Type: SSH_FXP_ATTRS, Size: 54, Number: 5895
. 2019-06-05 15:21:21.482 FILETOMOVE_20190524_010139.json;-;1203;2019-05-23T23:01:40.000Z;3;"root" [0];"root" [0];rwxrwxrwx;1
< 2019-06-05 15:21:21.482 Script: -rwxrwxrwx   0 root     root          1203 May 23 23:01:40 2019 FILETOMOVE_20190524_010139.json
> 2019-06-05 15:21:21.561 Script: mv "FILETOMOVE_20190524_010139.json" "Archive/FILETOMOVE_20190524_010139.json"
. 2019-06-05 15:21:21.561 Listing file "FILETOMOVE_20190524_010139.json".
> 2019-06-05 15:21:21.561 Type: SSH_FXP_LSTAT, Size: 44, Number: 6151
< 2019-06-05 15:21:21.577 Type: SSH_FXP_ATTRS, Size: 54, Number: 6151
. 2019-06-05 15:21:21.577 FILETOMOVE_20190524_010139.json;-;1203;2019-05-23T23:01:40.000Z;3;"root" [0];"root" [0];rwxrwxrwx;1
. 2019-06-05 15:21:21.577 Moving file "FILETOMOVE_20190524_010139.json" to "Archive/FILETOMOVE_20190524_010139.json".
> 2019-06-05 15:21:21.577 Type: SSH_FXP_RENAME, Size: 87, Number: 6418
< 2019-06-05 15:21:21.577 Type: SSH_FXP_STATUS, Size: 26, Number: 6418
< 2019-06-05 15:21:21.577 Status code: 0
< 2019-06-05 15:21:21.577 Script: FILETOMOVE_20190524_010139.json
> 2019-06-05 15:21:21.624 Script: stat -- "FILETOMOVE_20190524_010139.json"
. 2019-06-05 15:21:21.624 Listing file "FILETOMOVE_20190524_010139.json".
> 2019-06-05 15:21:21.624 Type: SSH_FXP_LSTAT, Size: 44, Number: 6663
< 2019-06-05 15:21:21.624 Type: SSH_FXP_ATTRS, Size: 54, Number: 6663
. 2019-06-05 15:21:21.624 FILETOMOVE_20190524_010139.json;-;1203;2019-05-23T23:01:40.000Z;3;"root" [0];"root" [0];rwxrwxrwx;1
< 2019-06-05 15:21:21.624 Script: -rwxrwxrwx   0 root     root          1203 May 23 23:01:40 2019 FILETOMOVE_20190524_010139.json

有帮助吗?

【问题讨论】:

  • 请提供会话日志文件! (Session.SessionLogPath)
  • 不,我要求提供会话日志。
  • 好的,抱歉。当问题再次出现时,我会尝试找回它。

标签: c# sftp azure-webjobs winscp winscp-net


【解决方案1】:

我假设你想在远程服务器上移动一个文件(你也在方法中删除一个本地文件)。

每次调用OpenSession() 时,您都会创建一个新的Session - 无需关闭/处置旧的。您应该只在构造函数中创建一次会话。这可能是个问题。

此外,如果目标已存在,则无法移动远程文件 - 您无需对此进行检查,但您可以通过其他方式处理。

考虑是否需要sessionOptions.AddRawSettings("ProxyPort", "0");

您可能会在会话日志中找到一些其他信息。

我已经根据您的代码创建了一个测试类。它适用于我 - 也适用于重复调用:

using System.Diagnostics;
using System.IO;
using WinSCP;

namespace WinSCPTest
{
    public class SftpRemoteMoveTester
    {
        const char SftpDirectorySeparatorChar = '/';

        static readonly SessionOptions SessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
            HostName = SftpConfig.HostName,
            UserName = SftpConfig.UserName,
            Password = SftpConfig.Password,
            SshHostKeyFingerprint = SftpConfig.SshHostKeyFingerprint,
        };

        Session _sftpSession;

        public SftpUploadTester()
        {
            _sftpSession = new Session();
        }

        public void MoveRemoteFile(
            string remoteSourceDirectoryPath,
            string fileName,
            string remoteDestinationDirectoryPath)
        {
            var remoteSourceFilePath = CombineSftpPath(remoteSourceDirectoryPath, fileName);
            var remoteDestinationFilePath = CombineSftpPath(remoteDestinationDirectoryPath, fileName);

            OpenSessionIfNeeded();

            if (!_sftpSession.FileExists(remoteSourceFilePath))
            {
                Debug.WriteLine("Remote source file does not exists -> return");
                return;
            }

            if (_sftpSession.FileExists(remoteDestinationFilePath))
            {
                Debug.WriteLine("Remote destination file already exists -> return");
                return;
            }

            _sftpSession.MoveFile(remoteSourceFilePath, remoteDestinationFilePath);

            if (_sftpSession.FileExists(remoteSourceFilePath))
            {
                Debug.WriteLine("File hasn't be moved from source dir");
            }

            if (_sftpSession.FileExists(remoteSourceFilePath))
            {
                Debug.WriteLine("File doesn't exists in destination dir");
            }
        }

        public void OpenSessionIfNeeded()
        {
            if (!_sftpSession.Opened)
            {
                Debug.WriteLine("Session is closed -> Open it.");
                _sftpSession.Open(SessionOptions);
            }
        }

        string CombineSftpPath(params string[] parts)
        {
            return Path.Combine(parts)
                .Replace(Path.DirectorySeparatorChar, SftpDirectorySeparatorChar);
        }
    }
}

该示例不包括您可能想要添加的任何 _sftpSession 处置。

【讨论】:

  • 嗨 Ulric,谢谢回复。
【解决方案2】:
> 2019-06-05 15:21:21.561 Script: mv "FILETOMOVE_20190524_010139.json" "Archive/FILETOMOVE_20190524_010139.json"
. 2019-06-05 15:21:21.561 Listing file "FILETOMOVE_20190524_010139.json".
> 2019-06-05 15:21:21.561 Type: SSH_FXP_LSTAT, Size: 44, Number: 6151
< 2019-06-05 15:21:21.577 Type: SSH_FXP_ATTRS, Size: 54, Number: 6151
. 2019-06-05 15:21:21.577 FILETOMOVE_20190524_010139.json;-;1203;2019-05-23T23:01:40.000Z;3;"root" [0];"root" [0];rwxrwxrwx;1
. 2019-06-05 15:21:21.577 Moving file "FILETOMOVE_20190524_010139.json" to "Archive/FILETOMOVE_20190524_010139.json".
> 2019-06-05 15:21:21.577 Type: SSH_FXP_RENAME, Size: 87, Number: 6418
< 2019-06-05 15:21:21.577 Type: SSH_FXP_STATUS, Size: 26, Number: 6418
< 2019-06-05 15:21:21.577 Status code: 0

正如你在日志中看到的,服务器没有报告任何错误。它返回状态码 0,表示成功。所以如果文件不动,那是服务器的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 2023-02-25
    相关资源
    最近更新 更多