SftpClient.UploadFile和SftpClient.WriteAllBytes有什么区别? [英] What is the difference between SftpClient.UploadFile and SftpClient.WriteAllBytes?

查看:76
本文介绍了SftpClient.UploadFile和SftpClient.WriteAllBytes有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用SSH.NET通过SFTP传输文件时,我观察到一些奇怪的行为.我正在使用SFTP将XML文件传输到另一个服务(我不控制)以进行处理.如果我使用SftpClient.WriteAllBytes,则该服务会投诉该文件无效的XML.如果我先写入一个临时文件,然后使用SftpClient.UploadFile,则传输成功.

I am observing some strange behaviour when I use SSH.NET to transfer files with SFTP. I am using SFTP to transfer XML files to another service (which I don't control) for processing. If I use SftpClient.WriteAllBytes the service complains the file is not valid XML. If I first write to a temporary file and then use SftpClient.UploadFile the transfer is successful.

发生了什么事?

使用.WriteAllBytes:

public void Send(string remoteFilePath, byte[] contents)
{
    using(var client = new SftpClient(new ConnectionInfo(/* username password etc.*/)))
    {
        client.Connect();
        client.WriteAllBytes(remoteFilePath, contents);
    }
}

使用.UploadFile:

public void Send(string remoteFilePath, byte[] contents)
{
    var tempFileName = Path.GetTempFileName();
    File.WriteAllBytes(tempFileName, contents);
    using(var fs = new FileStream(tempFile, FileMode.Open))
    using(var client = new SftpClient(new ConnectionInfo(/* username password etc.*/)))
    {
        client.Connect();
        client.UploadFile(fs, targetPath);
    }
}

修改: 威尔在评论中问我如何将XML转换为字节数组.我不认为这是相关的,但我又是一个问这个问题的人...:P

Will in the comments asked how I turn the XML into a byte-array. I didn't think this was relevant, but then again I'm the one asking the question... :P

// somewhere else:
// XDocument xdoc = CreateXDoc();

using(var st = new MemoryStream())
{
    using(var xw = XmlWriter.Create(st, new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true }))
    {
        xdoc.WriteTo(xw);
    }
    return st.ToArray();
}

推荐答案

我可以使用NuGet的SSH.NET 2016.0.0重现您的问题.但不适用于2016.1.0-beta1.

I can reproduce your problem using SSH.NET 2016.0.0 from NuGet. But not with 2016.1.0-beta1.

检查代码,我可以看到SftpFileStream(WriteAllBytes的用途)一直在不断写入相同(开始)的数据.

Inspecting the code, I can see that the SftpFileStream (what the WriteAllBytes uses) keeps writing the same (starting) piece of the data all the time.

您似乎正在遭受此错误的困扰:
https://github.com/sshnet/SSH.NET/issues/70

It seems that your are suffering from this bug:
https://github.com/sshnet/SSH.NET/issues/70

虽然错误描述不能清楚地表明这是您的问题,但修复该问题的提交与我发现的问题相匹配:
请考虑SftpFileStream.Write(byte []缓冲区中的偏移量,int偏移量,int计数),当不写入缓冲区时.修复了问题70.

While the bug description does not make it clear that it's your problem, the commit that fixes it matches the problem I have found:
Take into account the offset in SftpFileStream.Write(byte[] buffer, int offset, int count) when not writing to the buffer. Fixes issue #70.

要回答您的问题:方法的确应具有相似的行为.

To answer your question: The methods should indeed behave similarly.

除了SftpClient.UploadFile为上载大量数据而优化,而SftpClient.WriteAllBytes并非如此.因此,底层实现是非常不同的.

Except that SftpClient.UploadFile is optimized for uploads of large amount of data, while the SftpClient.WriteAllBytes is not. So the underlying implementation is very different.

此外,SftpClient.WriteAllBytes不会截断现有文件.重要的是,当您上传的数据少于现有文件的数据时.

Also the SftpClient.WriteAllBytes does not truncate an existing file. What matters, when you are uploading less data than the existing file have.

这篇关于SftpClient.UploadFile和SftpClient.WriteAllBytes有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆