Ali
Ali

Reputation: 684

Is there any way to zip a directory on Amazon S3 and downloading it dynamically in an app

I need to zip the files/folders which I have on Amazon S3 (if user clicks on the download a folder in my app). In other words, I need to create a zip file on the fly from files stored on S3 using php.

I have created a single download functionality in my php app, but my question is for multiple file download (as a zip file).

Thank you in advance

Upvotes: 0

Views: 911

Answers (1)

Ali
Ali

Reputation: 684

Yes, It seems that Amazon S3 does not provide this functionality and I had to do it myself in the app level. I used ZipArchive to read the files from S3 and zip them up on the server.

$client->registerStreamWrapper();

$files = $client->getIterator('ListObjects', [
    'Bucket' => $bucket,
    'Prefix' => $directory_name
]);

$file = get_temp_file();
$zip = new ZipArchive(); 
$zip->open($file, ZipArchive::CREATE);

foreach ($files as $f) {
    $contents = file_get_contents("s3://{$bucket}/{$f['Key']}");
    $zip->addFromString($file_name, $contents);
}

Upvotes: 2

Related Questions