Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stream files from S3 as ZIP file.
<?php
use Aws\S3\S3Client;
use ZipStream\ZipStream; // https://github.com/maennchen/ZipStream-PHP
use GuzzleHttp\Client as HttpClient;
protected function streamAsZip($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();
}
@JacobBennett

This comment has been minimized.

Copy link

@JacobBennett JacobBennett commented Feb 15, 2017

is this a Guzzle HttpClient?

@tillkruss

This comment has been minimized.

Copy link
Owner Author

@tillkruss tillkruss commented Feb 15, 2017

@JacobBennett: Yes, just added the missing import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment