Amazon S3 Bucket URL's fails to load some files programmatically

Viewed 202

Amazon S3 Bucket url's fails to load some files, while these files are browsed directly in browser it's loading fine. No issues at all. What I am trying to do is create a pdf from html using a library called wkhtmltopdf programmatically using PHP.

The bucket I am trying to access is publicly accessible. Some of these files which are failing is also not the same ones as the previous, it keep's on changing every time. So I am not able to tag any doubts on it. When I checked regarding the limits of requests to an Amazon S3 bucket, can send up to 5500 requests per second. And here in my case am sending a request of 1600 that's all images files.

Below is my code.

<?php
set_time_limit(120);
require './aws-autoloader.php';

require './Aws/S3/S3Client.php';
require './Aws/Exception/AwsException.php';

$s3 = new Aws\S3\S3Client([
    'version' => 'latest',
    'region' => 'us-east-1'
]);

?>
<html><body>

<?php 
$response = $s3 ->listObjects(array('Bucket' =>"my-example-bucket"));


$data = '<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <h1>Lorem Ipsum</h1>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod laboriosam atque, consequuntur beatae fugiat in incidunt perspiciatis dolorem ipsum nam itaque animi nobis quam illum dolore. Nam nostrum maiores, ipsa.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa vero veniam sed ullam commodi! Accusantium, officia vitae, adipisci mollitia, blanditiis voluptates accusamus nam atque repellendus laborum, ullam vel eligendi esse?</p>';

foreach($response['Contents'] as $key => $value) {
    
    $image = '';
    
    $path = $value['Key'];
    $image = "https://my-example-bucket.s3.amazonaws.com/$path";
    $data.='<img src="'.$image.'" width="100">';
    
?>
    
<?php           
}
$data.='</body>
</html>';


file_put_contents('C:\Users\Administrator\Demo\htdocs\test.html', $data);


exec('C:\"Program Files"\wkhtmltopdf\bin\wkhtmltopdf ./test.html ./test.pdf> C:\Users\Administrator\Downloads\my.txt 2>&1');
?>


</body></html>
2 Answers

I executed the below command to get all the files downloaded before starting to generate the pdf and now its taking only one minute to download almost 1650 image files of average size and in less than 2.5 seconds pdf is generated.

Below is my updated code

<?php
set_time_limit(300);


require './aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
$s3 = new Aws\S3\S3Client([
    'version' => 'latest',
    'region' => 'us-east-1'
]);

try {
    // Get the object.
    $response = $s3 ->listObjects(array('Bucket' =>"my-example-bucket",'MaxKeys' => 1600, 'Prefix' => 'example-prefix/'));
    
   
        
   exec('aws s3 cp s3://my-example-bucket/example-prefix/ C:/xampp/htdocs/test-folder/downloads/example-prefix/ --recursive > ./log.txt 2>&1');
   $data = '
    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>';
    foreach($response['Contents'] as $key => $value) {
        $path = $value['Key']; 
        $data.='<img src="http://localhost/test-folder/downloads/'.$path.'">';
    }
$data.='</body></html>';
file_put_contents('C:\xampp\htdocs\test-folder\test.html', $data);
   exec('C:\"Program Files"\wkhtmltopdf\bin\wkhtmltopdf ./test.html ./test.pdf > ./log.txt 2>&1');
   
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

?>
Related