-
-
Save bmadigan/9ab16d9674251f8d548fb74196a41514 to your computer and use it in GitHub Desktop.
Stream files from S3 as ZIP file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Aws\S3\S3Client; | |
use ZipStream\ZipStream; // https://github.com/maennchen/ZipStream-PHP | |
protected function streamPhotosetAsZip($files) | |
{ | |
$s3 = S3Client::factory('...'); | |
$zip = new ZipStream("foobar.zip"); | |
foreach ($files as $file) { | |
$request = $s3->createPresignedRequest( | |
$s3->getCommand('GetObject', [ | |
'Key' => $file->path, | |
'Bucket' => 'bucket-name', | |
]), | |
'+20 seconds' | |
); | |
$tmpfile = tempnam(sys_get_temp_dir(), str_random()); | |
(new HttpClient)->request('GET', (string) $request->getUri(), ['sink' => fopen($tmpfile, 'w+')]); | |
$fp = fopen($tmpfile, 'r'); | |
$zip->addFileFromStream(basename($file->path), $fp); | |
fclose($fp); | |
} | |
$zip->finish(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment