AWS S3 Internal Server Error: Tyring to upload pdf file on after another

I’m trying to generate PDF file using FPDF and PHP and later upload to AWS-s3 and generate the url. When I executed the below code in my local-machine Using XAMPP it’s generating files and uploading it to S3.

But When I deployed in AWS server as an API it’s uploading only first file and for other its giving 500 server error

Below is my code that I used in Local Machine

    require '../fpdf/fpdf.php';
    require 'start.php';

    use Aws\Exception\AwsException;

    $location = "bangalore";
    $db     =   getConnection();
    $query   = "SELECT first_name FROM customer_info where location = '$location'";
    $execute = $db->query($query);
    $result  = $execute->fetchAll(PDO::FETCH_ASSOC);

  for($i = 0; $i < $len; $i++) {
 $pdf = new FPDF();
 $pdf->AddPage();

$pdf->SetFont('Arial', 'B', 14);
$txt = "Legal Document of ".$result[$i]['first_name'];
$pdf->Cell(180, 0, $txt, 0, 1, 'C');
$pdf->Line(5, 20, 200, 20);

$docname = $result[$i]['first_name'] . ".pdf";
$filepath = "../file/{$docname}";
$pdf->Output($filepath, 'F');

//s3 client
try {
       $res = $S3->putObject([
        'Bucket' => $config['S3']['bucket'],
        'Key' => "PATH_TO_THE_DOCUMENT/{$docname}",
        'Body' => fopen($filepath, 'rb'),
        'ACL' => 'public-read'
    ]);

    var_dump($res["ObjectURL"]);
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

}

Output:

   Array ( [0] => Array ( [first_name] => Mohan ) [1] => Array ( [first_name] => Prem ) [2] => Array ( [first_name] => vikash ) [3] => Array ( [first_name] => kaushik ) )

     string(70) "https://streetsmartb2.s3.amazonaws.com/PATH_TO_THE FILE/Mohan.pdf" 
  
 string(72) "https://streetsmartb2.s3.amazonaws.com/PATH_TO_THE FILE/Prem%20.pdf" 

API CODE

      //pdf generation another api
      $app->post('/pdfmail', function() use($app){
 
    //post parameters
    $location = $app->request->post('loc');
    $id       = $app->request->post('id');



   $db = getConnection();
   $query   = "SELECT * FROM customer_info where location = '$location'";
  $execute = $db->query($query);
  $result  = $execute->fetchAll(PDO::FETCH_ASSOC);
  $len = sizeof($result);

  //request array
  $request = array();

  if($result != Null) {
      for($i = 0; $i < $len; $i++) {
          $pdf = new FPDF();
          $pdf->AddPage();

          $pdf->SetFont('Arial', 'B', 14);
          $txt = "Document of Mr." . $result[$i]['first_name'];
          $pdf->Cell(180, 0, $txt, 0, 1, 'C');
          $pdf->Line(5, 20, 200, 20);

          $docname = $result[$i]['first_name'] . ".pdf";
          var_dump($docname);
          $filepath = "../file/{$docname}";
          var_dump($filepath);
          $pdf->Output($filepath, 'F');


          //s3 client
          require '../aws/aws-autoloader.php';

          $config = require('config.php');


          //create s3 instance
          $S3 = S3Client::factory([
              'version' => 'latest',
              'region' => 'REGION',
              'credentials' => array(
                  'key' => $config['S3']['key'],
                  'secret' => $config['S3']['secret']
              )
          ]);



          try {
              $res = $S3->putObject([
                  'Bucket' => $config['S3']['bucket'],
                  'Key' => "PATH_TO_FILE{$docname}",
                  'Body' => fopen($filepath, 'rb'),
                  'ACL' => 'public-read'
              ]);
              var_dump($res["ObjectURL"]);
          } catch (S3Exception $e) {
              echo $e->getMessage() . "\n";
          }

      }
  }

OUTPUT WHEN TESTED IN POSTMAN

    string(10) "vikash.pdf"
    string(18) "../file/vikash.pdf"
   string(71) "https://streetsmartb2.s3.amazonaws.com/PATH_TO_FILE/vikash.pdf"
  string(13) "pradeepan.pdf"
   string(21) "../file/pradeepan.pdf"

After this I’m getting internal server error.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.