Hi, I'm new to Perl.
I try to check Last Upload in perl on Amazon S3 but the script failed to print/recognize last successful upload date, here is the code:

#!/usr/bin/perl use DateTime; use Time::Local; $s3_bucket = 'some/folder/'; $state_failure = 1; @checkB = &checkBackup; $checkBT = checkBackup(latest); print "CHECKBT: $checkBT\n"; $dt = DateTime->now; $ymdh = $dt->ymd . "-" . $dt->hour; $ymdh = converttoEpoch($ymdh); if ($ymdh - $checkB[0] > 24){ print "ERROR\n"; $state_failure = 1; } else { print "SUCCESS"; $state_failure = 0; } sub checkBackup { local ($req) = @_; @last_backups = (); @backupList = (); @last_backups = `s3cmd --access_key=XXX --secret_key=xxx ls s3 +://$s3_bucket \| awk \'\{print \$1\" \"\$2\}\'`; $i = 0; foreach $lineB (@last_backups) { chomp $lineB; if ($i > 0){ #print "Before Convert: $lineB\n"; $lineBC = converttoEpoch($lineB); #print "After convert: $lineB\n"; push (@backupListB, $lineB); push (@backupList, $lineBC); } $i++; } @sortedList = sort {$b <=> $a} @backupList; @sortedListB = sort {$b <=> $a} @backupListB; if ($req eq "latest") { return $sortedListB[0]; } else { return @sortedList; } } sub converttoEpoch { local ($input) = @_; @all_num = $input =~ /(\d+)/g; $yearE = $all_num[0]; $monthE = $all_num[1]; $dayE = $all_num[2]; $hourE = $all_num[3]; $minuteE = 0; $secondE = 0; if (@all_num){ $dt = DateTime->new( year => $yearE, month => $mont +hE, day => $dayE, hour => $hourE, minute => 0, second => 0, nanosecond => 000000000, time_zon +e => 'local' ); $epoch_time = $dt->epoch; $epoch_time = $epoch_time / 3600; return $epoch_time; } } use Mail::Sender; $sender = new Mail::Sender { # smtp => 'some.mail.provider', on_errors => 'code', debug_level => 1, } or die "Can't create the Mail::Sender object: $Mail::Sender::Error\n +"; if ($state_failure == 1){ if ($sender->MailMsg( { to => 'account@some.mail.provider', subject => "NOTICE - No Additional Backup is c +reated on Amazon S3", msg => "There is no Backup activity more than +24 hour\nLast Backup Time: $checkBT\n", } ) < 0 ) { die "$Mail::Sender::Error\n"; } }

Result: I got email and the script print the first upload date (the oldest) file in Amazon S3, not the latest date (newest date).
Please help, thank you very much.


In reply to To Check Last Upload Date on Amazon S3 by babah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.