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

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

我正在尝试使用 FPDF 和 PHP 生成 PDF 文件,然后上传到 AWS-s3 并生成 url. 当我在 local-machine 中执行以下代码时使用 XAMPP 它正在生成文件并将其上传到 S3.

但是当我在 AWS 服务器中作为 API 部署时,它只上传第一个文件,而对于其他文件,它给出 500 服务器错误

下面是我在本地机器中使用的代码

    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";
}

}

输出:

   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 代码

      //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";
          }

      }
  }

在 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"

在此之后我收到内部服务器错误。

我没有使用 require_once,而是使用 require...所以当代码遍历时,它一次又一次地创建 AWS Class。

代码

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

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


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