简体   繁体   English

Perl-Net :: SFTP :: Foreign

[英]Perl - Net::SFTP::Foreign

I am trying to write an upload script in Perl , using Net::SFTP::Foreign. 我正在尝试使用Net :: SFTP :: Foreign在Perl中编写一个上传脚本。

I am having issue with checking if the directory exists, and if it does not, creating it. 我在检查目录是否存在时遇到问题,如果不存在,则创建它。

Net::SFTP::Foreign seems to just error and close the connection if the directory doesn't exist, and never runs the "else" Net :: SFTP :: Foreign似乎只是错误,如果目录不存在,则关闭连接,并且从不运行“ else”

You can see the code below, can aanyone see where im going wrong? 您可以看到下面的代码,任何人都可以看到我在哪里出错吗?

sub uploadtoftp
{
use Net::SFTP::Foreign;
use File::Basename;
use warnings;

  my $host=$_[0];
  my $user=$_[1];
  my $pw=$_[2];


   my $home_directory ="/home/testuser";
  my $remote_path=$home_directory."/".$name."/".$destination_dir;



  if (my $ftp = Net::SFTP::Foreign->new($host,
                                   user => $user,
                                   password => $pw,
                                   autodie => 0))
  {

      my $destination_dir_proceed=0;


        $ftp->find("$remote_path", on_error => sub { print "Creating directory\n"; $ftp->mkdir("$remote_path")  });
        print $ftp->error;


        if($ftp->opendir($remote_path)) 
        {
          $destination_dir_proceed=1;
        }


        if($destination_dir_proceed==1) 
        {
        # --- loop through file list and upload all new files
        foreach $filename (split(/ /, $file_list)) 
        {
          $ftp->put($filename,$remote_path.$filename);

        }


        }

      else
      {
        print "cannot reach directory $remote_path\n";
      }


  }




} # end subroutine uploadtoftp()

When you instantiated the Net::SFTP::Foreign object, you created it with autodie => 1 . 实例化Net :: SFTP :: Foreign对象时,使用autodie => 1创建了它。

Autodie will "promote non-recoverable errors to execptions automatically". Autodie将“将不可恢复的错误自动促进执行”。

You can remove the autodie line and be OK as long as you're checking for status regularly throughout the script. 只要您在整个脚本中定期检查状态,就可以删除自动冲模行并确定。

If there are places you want to die at, you can use die_on_error after you've made a request 如果您想死在某个地方 ,可以在提出请求后使用die_on_error

You can use the following logic to create the folder: 您可以使用以下逻辑来创建文件夹:

$ftp->find("$remote_path", on_error => sub { print "Creating directory\n"; $ftp->mkdir("$remote_path") });

if you do not need print statement: 如果不需要打印语句:

$ftp->find("$remote_path", on_error => sub { $ftp->mkdir("$remote_path") });

If you need to create all missing folders in the path, this should work (I cannot currently test it, but the idea seems correct). 如果您需要在路径中创建所有丢失的文件夹,这应该可以工作(我目前无法对其进行测试,但是这个想法似乎是正确的)。

my $home_directory ="home/testuser";
my $remote_path=$home_directory."/".$name."/".$destination_dir;

my $currentPath = '';
foreach my $directory (split '/', $remote_path) {
    $currentPath = "$currentPath/$directory";
    $ftp->find("$currentPath", on_error => sub { print "Creating directory\n"; $ftp->mkdir("$currentPath") });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM