How to Download Multiple S3 File and Zip them together

This is a simple tutorial on how to get AWS S3 object and zip them together.

This snippet will allow you to download multiple S3 files and Zip them together for an easy one download file. 

$files_to_zip = array();
$files = array('aws_file_key1.pdf', 'aws_file_key2.pdf', 'aws_file_key3.pdf');
foreach ($files as $f) {

		$path_parts = pathinfo($aws_filekey);
		$files_to_zip[] = DOWNLOAD_PATH . $path_parts['basename'];
		
		$result = $s3->getObject(array(
		    'Bucket' => AWS_BUCKET,
		    'Key'    => urldecode($aws_filekey),
		    'SaveAs' => DOWNLOAD_PATH . $path_parts['basename'] 
		));


}

$zipname =  'file.zip';
$zip = new ZipArchive;
$zip->open(DOWNLOAD_PATH . $zipname, ZipArchive::CREATE);
foreach ($files_to_zip as $file) {
  $path_parts = pathinfo($file);
  $zip->addFile($file, 'files/' .$path_parts['basename']);
}
$zip->close();