Skip to content

Instantly share code, notes, and snippets.

@ranacseruet
Last active July 28, 2022 19:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ranacseruet/9167580 to your computer and use it in GitHub Desktop.
Save ranacseruet/9167580 to your computer and use it in GitHub Desktop.
Retrieve Multiple files from Amazon S3
<?php
namespace S3;
use Aws\S3\S3Client;
use \Aws\Common\Exception\TransferException;
/**
* Description of S3Client
* @author Rana
* @link http://codesamplez.com/programming/amazon-s3-get-multiple-objects-php
*/
class MyS3Client extends S3Client
{
//put your code here
public static function getObjects(Array $configs, S3Client $client)
{
$requests = array();
$savePaths = array();
foreach ($configs as $config) {
$url = "https://".$config["Bucket"].".s3.amazonaws.com/".$config["Key"];
$request = $client->get($url);
$requests[] = $request;
$savePaths[$url] = $config["saveAs"];
}
try {
$responses = $client->send($requests);
}
catch(TransferException $e) {
echo $e->getError();
}
foreach ($responses as $res) {
$localPath = $savePaths[$res->getEffectiveUrl()];
file_put_contents($localPath, $res->getBody(true));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment