Skip to content

Instantly share code, notes, and snippets.

@jeremeamia
Created August 7, 2014 17:40
Show Gist options
  • Save jeremeamia/59934cfe293ce76a8deb to your computer and use it in GitHub Desktop.
Save jeremeamia/59934cfe293ce76a8deb 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