使用winscp session从SFTP进行流式传输。getFile失败– ((winscp.pipestream)流).length'投掷了类型的例外。
我的应用需要使用SFTP从位置将文件直接复制到Azure存储。 我们的应用程序与.NET 4.6一起使用C#,我们的WINSCP版本为5.21.1。
我的旧代码使用 session.getFileTodirectory()
方法起作用,但问题在于它需要将文件存储在我们托管内部的temp文件夹上。
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
var transfer = session.GetFileToDirectory(FilePath, fullPath);
using (Stream stream = File.OpenRead(transfer.Destination))
{
UploadToAzure(stream, Filename, Foldername);
}
}
按照我们计划完全使用Azure存储,我
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
using (Stream stream = session.GetFile(FilePath, transferOptions))
{
UploadToAzure(stream, Filename, Foldername);
}
}
在这里更改了我的代码,我的库将文件使用 stream
上传到Azure。
使用我的旧代码,该代码正常工作,该代码仍将其保存到Temp文件夹之前,然后发送到Azure。
public static string UploadToAzure(Stream attachment, string Filename, string Foldername)
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
var connectionString = $"{ConfigurationManager.AppSettings["AzureFileShareConnectionString"]}";
string shareName = $"{ConfigurationManager.AppSettings["AzureFileShareFolderName"]}";
string dirName = $"files\\{Foldername}";
string fileName = Filename;
try
{
ShareClient share = new ShareClient(connectionString, shareName);
share.CreateIfNotExists();
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
directory.CreateIfNotExists();
// Get a reference to a file and upload it
ShareFileClient file = directory.GetFileClient(fileName);
file.Create(attachment.Length);
file.UploadRange(
new HttpRange(0, attachment.Length), attachment);
}
catch (Exception e)
{
return $"Uploaded {Filename} failed : {e.ToString()}";
}
return $"{Filename} Uploaded";
}
但是目前我的新代码无法使用错误消息
'((winscp.pipestream)流)。lengtth'抛出了类型“ system.notsupportedException”的例外。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
stream
winscp sessig 不能实现stream.length
属性,因为WinSCP无法保证文件的大小已固定。下载文件时,远程文件可能正在更改。更不用说ASCII传输模式了,当文件转换时文件转换时,对最终尺寸的影响不可预测。您在两个位置使用大小(
stream.length
):创建文件时:
sharefileclient.create 是
maxSize
。因此,它看起来并不是一个实际尺寸。您可能只在这里放一个任意的大数。或者如果您喜欢(并且知道文件没有更改),请使用
session.getFileInfo
和 :上传内容时:
以上可以用简单
sharefileclient.upload
:The
Stream
returned by WinSCPSession.GetFile
does not implement theStream.Length
property, because WinSCP cannot guarantee that the size of the file is fixed. The remote file might be changing while you are downloading the file. Not to mention ASCII transfer mode, when the file is converted while being transferred, with unpredictable impact on the final size.You use the size (
Stream.Length
) in two places:When creating the file:
The parameter of
ShareFileClient.Create
ismaxSize
. So it does not look like it's a real size. You can possibly just put an arbitrary large number here.Or if you prefer (and know that the file is not changing), read the current size of the remote file using
Session.GetFileInfo
andRemoteFileInfo.Length
:When uploading the contents:
The above can be replaced with simple
ShareFileClient.Upload
: