Skip to content

Instantly share code, notes, and snippets.

@aethant
Forked from jeremeamia/build-zip-from-s3.php
Created May 25, 2016 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aethant/6f0fa802a4ff1c5bea74ba1209c03a18 to your computer and use it in GitHub Desktop.
Save aethant/6f0fa802a4ff1c5bea74ba1209c03a18 to your computer and use it in GitHub Desktop.
Create a zip from objects from S3.
<?php
// These are just the basics for how to do this. Notes:
// - Not fully tested.
// - Not suitable for production.
// - May not work well if large ammounts of data.
// - Requires AWS SDK for PHP and http://php.net/manual/en/book.zip.php
require '/path/to/sdk-or-autoloader';
$s3 = Aws\S3\S3Client::factory(/* sdk config */);
$s3->registerStreamWrapper();
$zip = new ZipArchive;
$zip->open('/path/to/zip-you-are-creating.zip', ZipArchive::CREATE);
$bucket = 'your-bucket';
$objects = $s3->getIterator('ListObjects', array(
'Bucket' => $bucket,
// other params...
));
foreach ($objects as $object) {
$contents = file_get_contents("s3://{$bucket}/{$object['Key']}");
$zip->addFromString($object['Key'], $contents);
}
$zip->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment