英文:
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;
}
}
}
这样,您可以:
- 确认问题是否仍然存在
- 检查错误
- 检查
TransferOperationResult
。
双重检查批处理文件中是否存在任何格式化时间戳选项,即任何设置了 %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:
- confirm the issue persists
- check for errors
- inspect the
TransferOperationResult
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论