Guest User

Untitled

a guest
Dec 7th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.92 KB | None | 0 0
  1.         my $fh;
  2.             if (!open($fh, '<', $prefix.$source)) {
  3.                 print logDate()."Unable to open $source for reading: $!\n";
  4.                 return 0;
  5.             }
  6.             binmode($fh);
  7.  
  8.             my $contentLength = -s $source;
  9.             print logDate()."contentLength: $contentLength\n" if ($this->{'verbose'}>1);
  10.  
  11.             my $md5 = Digest::MD5->new();
  12.  
  13.             my $startTime = time();
  14.  
  15.             my $req = $this->makeRequest('PUT', $target.$source, {'x-amz-meta-lstat'=>join(':', @$lstat)}, {'Content-Length'=>$contentLength});
  16.             print logDate()."request : \n".Dumper($req) if ($this->{'verbose'}>1);
  17.  
  18.             print logDate()."writing to buffer\n" if ($this->{'verbose'}>1);
  19.  
  20.            
  21.             my $uTime = time();
  22.  
  23.             $ua->do_request(
  24.                 request    => $req,
  25.                 # Probably duplicating a load of logic here :(
  26.                 host       => $host,
  27.                 port       => $port || $url->scheme || 80,
  28.                 SSL        => $url->scheme eq 'https' ? 1 : 0,
  29.  
  30.                 # We override the default behaviour (pulling content from HTTP::Request) by passing a callback explicitly
  31.                 request_body => sub {
  32.                     my ($stream) = @_;
  33.  
  34.                     # This part is the important one - read some data, and eventually return it
  35.                     my $read = sysread $fh, my $buffer, 32768;
  36.                     $md5->add($buffer);
  37.  
  38.                     print "o" if ($this->{'verbose'}>5);
  39. #                   if ($this->{'bwlimit'}) {
  40. #                       my $myBW = $bytesRead/(time()-$uTime)/1000000;
  41. #                       if ($myBW>$this->{'bwlimit'}) {
  42. #                           my $usleep = ((1000/$this->{'bwlimit'})-(1000/($myBW-$this->{'bwlimit'})))*1000*1.1;
  43. #                           usleep($usleep) if ($usleep>0 && $contentLength>1000000);
  44. #                       }
  45. #                   }
  46.                     $uTime = time();
  47.  
  48.                     return $buffer if $read;
  49.  
  50.                     # Don't really need to close here, but might as well clean up as soon as we're ready
  51.                     close $fh or warn $!;
  52.                     undef $fh;
  53.                     return;
  54.                 },
  55.  
  56.                 on_response => sub {
  57.                     my ($response) = @_;
  58.                     if($fh) {
  59.                         close $fh or die("Unable to close $target: $!");
  60.                     }
  61.                     $this->{'loop'}->remove($ua);
  62.                     $this->{'loopCnt'}--;
  63.                     if ($response->code == 500) {
  64.                         push(@{$this->{'todo'}}, $source);
  65.                         return;
  66.                     }
  67.                    
  68.                     print "\n" if ($this->{'verbose'});
  69.                    
  70.                     my $digest = $md5->hexdigest();
  71.                     my $etag = $response->header('ETag');
  72.                     $etag =~ s/"//g;
  73.                     $response->header('ETag', $etag);
  74.                     die ("noEtag".Dumper($response)) if (!$response->header('ETag'));
  75.                     die ("MD5 Sum ".$digest." did not match ".$response->header('ETag')."!") if ($digest ne $response->header('ETag'));
  76.  
  77.                     my $diffTime = time()-$startTime;
  78.                     $diffTime=1 if (!$diffTime);
  79.                     print logDate()."completed: ".sprintf("%.2f", $contentLength*8/1000000/$diffTime)."Mb/s\n\n" if ($this->{'verbose'});
  80.                 },
  81.  
  82.                 on_error => sub {
  83.                     my ( $message ) = @_;
  84.                     if($fh) {
  85.                         close $fh or die $!;
  86.                     }
  87.  
  88.                     $this->{'loop'}->remove($ua);
  89.                     $this->{'loopCnt'}--;
  90.                     print STDERR "Failed - $message\n";
  91.                     # Could do a $loop->loop_stop here - some failures should be fatal!
  92.                 }
  93.             );
Add Comment
Please, Sign In to add comment