重复文件将以文件名加上DD_MM_YY_HH_mm_SS的方式上传。

huangapple go评论82阅读模式
英文:

winscp file upload duplicate files are uploaded with name of file plus DD_MM_YY_HH_mm_SS

问题

以下是翻译好的部分:

"我正在使用以下代码将文件上传到SFTP:

using (Session session = new Session())
{
    session.Open(sessionOptions);

    using var s = new MemoryStream(Encoding.UTF8.GetBytes(content ?? ""));
    TransferOptions o = new TransferOptions { OverwriteMode = OverwriteMode.Overwrite };
    session.PutFile(s, remoteFilePath, o);

    session.Close();
    result = true;
}

有时,无缘无故地我会看到以时间后缀(DD_MM_YY_HH_mm_ss)为后缀的重复文件被上传。

有什么想法可能是问题的原因?"

英文:

I am using following code to upload the file to sftp

using (Session session = new Session())
                {
                    
                    session.Open(sessionOptions);

                    using var s = new MemoryStream(Encoding.UTF8.GetBytes(content ?? ""));
                    TransferOptions o = new TransferOptions { OverwriteMode= OverwriteMode.Overwrite };
                    session.PutFile(s, remoteFilePath, o);

                    session.Close();
                    result = true;
                }

sometime for no reason i see duplicate file with suffix as per time (DD_MM_YY_HH_mm_ss) uploaded

any idea what could be the problem

答案1

得分: 1

我会再次尝试上传过程,使用官方示例

using System;
using WinSCP;
 
class Example
{
    public static int Main()
    {
        try
        {
            // 设置会话选项
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
            };
 
            using (Session session = new Session())
            {
                // 连接
                session.Open(sessionOptions);
 
                // 上传文件
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
 
                TransferOperationResult transferResult;
                transferResult =
                    session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
 
                // 检查任何错误
                transferResult.Check();
 
                // 打印结果
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Console.WriteLine("上传 {0} 成功", transfer.FileName);
                }
            }
 
            return 0;
        }
        catch (Exception e)
        {
            Console.WriteLine("错误: {0}", e);
            return 1;
        }
    }
}

这样,您可以:

双重检查批处理文件中是否存在任何格式化时间戳选项,即任何设置了 %TIMESTAMP% 环境变量的选项。

英文:

I would try again the upload process, using the official example

using System;
using WinSCP;
 
class Example
{
    public static int Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
            };
 
            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);
 
                // Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
 
                TransferOperationResult transferResult;
                transferResult =
                    session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
 
                // Throw on any error
                transferResult.Check();
 
                // Print results
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                }
            }
 
            return 0;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
            return 1;
        }
    }
}

That way, you can:

Double-check for any formatting timestamp option in Batch File, i.e. any %TIMESTAMP% environment variable set.

答案2

得分: 0

我所做的是,不再逐个调用,而是在循环中执行操作,而且不是每次都打开会话,而是在未打开时才打开。

using (Session session = new Session())
{
    if (!session.Opened)
    {
        session.Open(sessionOptions);
    }

    foreach (var item in newContents)
    {
        try
        {
            if (!string.IsNullOrWhiteSpace(_config.DefaultPath))
            {
                item.SftpFileName = _config.DefaultPath + item.SftpFileName;
            }

            using var s = new MemoryStream(Encoding.UTF8.GetBytes(item.Content ?? ""));
            
            session.PutFile(s, item.SftpFileName, o);

            item.IsSuccess = true;
        }
        catch (Exception exception)
        {
            _logger.LogError(exception, $"Failed in uploading file to using winscp [{item.SftpFileName}]");
        }
    }
    session.Close();
}
英文:

what i did is instead of separate one by one call i done thing in a loop and instead of opening session each time i open it when it is not opened

using (Session session = new Session())
                {
                    if (!session.Opened)
                    {
                        session.Open(sessionOptions);
                    }

                    foreach (var item in newContents)
                    {

                        try
                        {
                            if (!string.IsNullOrWhiteSpace(_config.DefaultPath))
                            {
                                item.SftpFileName = _config.DefaultPath + item.SftpFileName;
                            }

                            using var s = new MemoryStream(Encoding.UTF8.GetBytes(item.Content ?? ""));

                            session.PutFile(s, item.SftpFileName, o);

                            item.IsSuccess= true;
                        }
                        catch (Exception exception)
                        {
                            _logger.LogError(exception, $"Failed in uploading file to using winscp [{item.SftpFileName}]");
                        }
                    }
                    session.Close();
                }

huangapple
  • 本文由 发表于 2023年7月12日 22:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定