【问题标题】:Upload file to subdirectory with specific name when subdirectories don't exist?当子目录不存在时,将文件上传到具有特定名称的子目录?
【发布时间】:2014-11-16 20:53:51
【问题描述】:

也许这是一个由四部分组成的问题:

  1. 上传到子目录
  2. 子目录不存在
  3. 使用与本地文件不同的远程文件名
  4. 子目录应该有明确的权限(类似于WinSCP .NET assembly - How to set folder permissions after creating directory?中的root问题)

尝试过:

var localPath = Path.GetTempFileName();
var remoteFolder = "/some/directory/beneath/root";
var slash = "/"; // maybe given as '/' or '\'...
var remotePath = remoteFolder + slash + "destination.ext1.ext2.txt";
var session = new Session(sessionOptions);
var result = session.PutFiles(localPath, remotePath, false, new FileTransferOptions { FilePermissions = new FilePermissions { Octal = "700" }, KeepTimestamp..., etc });
result.Check();

抛出异常Cannot create remote file '/some/directory/beneath/root/destination.ext1.ext2.txt'. ---> WinSCP.SessionRemoteException: No such file or directory.

我终于能够通过the crazy workaround indicated here 在我的临时路径中创建子目录结构并在第一个文件夹上使用PutFiles 来创建具有正确权限的子目录:

var tempRoot = Path.GetTempPath();
var tempPath = Path.Combine(tempRoot, remoteFolder);
Directory.CreateDirectory(tempPath);

// only need to upload the first segment, PutFiles will magically grab the subfolders too...
var segment = remoteFolder.Substring(0, remoteFolder.IndexOf(slash, StringComparison.Ordinal));
if( !this.DoesFolderExist(segment) )
{
        // here's the workaround...
        try
        {
            this._session.PutFiles(Path.Combine(tempRoot, segment), segment, false, new TransferOptions { FilePermissions = this._transferOptions.FilePermissions }).Check();
        }
        catch (InvalidOperationException)
        {
            // workaround for bug in .NET assembly prior to 5.5.5/5.6.1 beta
            // although I never hit this catch, maybe I've got a new enough version?
        }
}
Directory.Delete(tempPath); // finish workaround

但这太不直观了。

【问题讨论】:

  • 问题不清楚。目标目录/some/directory/beneath/root/ 是否存在?你能详细说明你的四点吗?
  • 每点 #2 -- 子目录不存在。什么不清楚?我想上传一个文件到一个不存在的子目录,但是使用不同的文件名,并确保它存在的文件和目录具有相同的权限。
  • 什么子目录? /some/directory/beneath/root/ 必须存在。我不明白确保它存在的文件和目录具有相同的权限

标签: c# file-permissions winscp winscp-net


【解决方案1】:

ad 1) WinSCP 不会(通常)创建上传的目标目录。它必须在上传之前存在。您可以使用Session.FileExists 测试是否存在,如果不存在,则使用Session.CreateDirectory 创建目录。当然,如果需要,WinSCP 会创建您要上传的目录。

ad 3) 您在Session.PutFilesremotePath 参数中指定不同的目标名称:

session.PutFiles(@"C:\path\original.txt", "/home/user/newname.txt");

ad 4) 您使用TransferOptions.FilePermissions 指定上传文件/目录的权限。请注意,WinSCP 隐式地将x 权限添加到每个组的目录,其中授予r 权限。所以当你为批量上传指定600权限时,600用于文件,而700用于目录。如果需要对不同的文件/目录使用不同的权限,需要一一上传。

【讨论】:

  • 我认为让我感到困惑的是“WinSCP 不会(通常)创建目标目录”和“WinSCP 当然会创建您正在上传的目录”之间的矛盾。在单击之前,我必须重新阅读几次——目标目录必须存在,因此您可以一个接一个地创建它们 (CreateDirectory),或者利用第二条语句在本地创建子目录 (只是空文件夹)然后上传它们以同时创建它们(PutFiles(localRoot, remoteRoot))。
  • 重点是如果你上传C:\folder\*/home/user/folder/,目标目录/home/user/folder/必须存在。但是如果C:\folder(匹配掩码,如果与*不同)中有子目录(是否为空),如C:\folder\a,则会在目标文件夹中自动创建,即/home/user/folder/a(除非当然,它们已经存在)。
猜你喜欢
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 2020-04-11
  • 2021-01-04
  • 1970-01-01
  • 2021-06-24
相关资源
最近更新 更多