Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

csv - PHP S3 download: echo huge text variable to browser with header to download

PHP S3 download: echo huge text variable to browser with header to download, but the page is getting timeout and not responding.

Here is my code to download a huge CSV file data from S3 services:

$result = $s3->getObject($_REQUEST['bucket'], $_REQUEST['path'], false);

if($result){
    header('Content-type: ' . $result->headers['type']);
    header('Content-Length: ' . $result->headers['size']);
    header('Content-Disposition: inline; filename="'.$_REQUEST['filename'].'"');

   echo $result->body;
} else {
    echo 'File not found';
}

I have also increased page execution time:

ini_set('memory_limit', '1024M' );
ini_set('max_execution_time', 0); 
question from:https://stackoverflow.com/questions/65940654/php-s3-download-echo-huge-text-variable-to-browser-with-header-to-download

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have found an alternate solution to above problem, instead of getting contents from aws and then buffering large amount of data to browser, i created "Public" url from the file and redirect user to the link.

// Getting the URL to an object
$url = $s3->getObjectUrl($targetBucket, $keyname);
// redirect user to the download link
header('Location: '.$url); exit;

If "Private" bucket and files, we can create a temporary bucket and copy the file to that bucket on request, like something below, and then again create link like above.

// temporary bucket for public files
$sourcebucket = '........';
$targetBucket = '........';
$keyname = '........';
// Copy an object.
$s3->copyObject([
    'Bucket'     => $targetBucket,
    'Key'        => "{$keyname}",
    'CopySource' => "{$sourcebucket}/{$keyname}",
]);
$s3->putObjectAcl(array(
    'Bucket'    => $targetBucket,
    'Key'       => $keyname,
    'ACL'       => 'public-read'
));

I posted here, may be it will help other's. As i was tired for solution! :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...