【问题标题】:upload file to SFTP from memory stream从内存流上传文件到 SFTP
【发布时间】:2013-02-13 07:12:12
【问题描述】:

如何将文件从内存流上传到 SFTP 服务器。本地路径不可用,因为我正在从 azure blob 上传文件。因为我得到了 .NET 不支持 SFTP 协议的信息,所以尝试了 3rd 方 dll,例如“SharpSSH”、“Routrek.granados”和“WinSCP”。但没有一个适合我的场景。即在put方法中不支持bite[]或stream。

任何人都可以向我推荐适合我的情况的免费 dll 或我可以处理的方式。

提前致谢。

【问题讨论】:

  • 我不了解免费软件,但我们的 SecureBlackbox 可以处理流,包括内存流。
  • 免费软件有严格要求吗?一些商业 SFTP 库支持从流上传,例如来自 Eldos 或 Rebex SFTP 的一个。以下链接显示了如何使用我们的库进行此操作:rebex.net/sftp.net/features/single-file-operations.aspx#stream

标签: asp.net c#-4.0 azure sftp


【解决方案1】:

您可以继续使用 WinSCP 之类的解决方案,但不要尝试使用 MemoryStream / byte[],只需先在本地下载文件:

var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("temp");
var blob = container.GetBlobReferenceFromServer("myblob.zip");

// This assumes you're using a Cloud Service and have a local resource called files
var dropFolder = RoleEnvironment.GetLocalResource("files").RootPath;
var filePath = Path.Combine(dropFolder, "myblob.zip");

// Download blob to a local resource first.
using (var fs = new FileStream(filePath, FileMode.Create))
{
    blob.DownloadToStream(fs);
}

var proc = new Process();
proc.StartInfo.FileName = "winscp.com";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.StandardInput.WriteLine("option batch abort");
proc.StandardInput.WriteLine("option confirm off");
proc.StandardInput.WriteLine("open mysession");
proc.StandardInput.WriteLine("ls");
proc.StandardInput.WriteLine("put " + filePath);
proc.StandardInput.Close();

【讨论】: