Home » Php » php – Amazon s3 batch upload

php – Amazon s3 batch upload

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have been trying to do batch upload using s3 client execute() method. It keeps throwing exception:

Message: Argument 1 passed to Aws\AwsClient::execute() must implement interface Aws\CommandInterface, array given, called in….

Even after following the example code from the doc.

Check this example maybe I am doing something wrong:

$bucket   = 'myBucket';
$commands = [];

$s3 = new Aws\S3\S3Client([
    'version' => 'latest',
    'region'  => 'us-west-1',
]);

$commands[] = $s3->getCommand('PutObject', [
    'Bucket' => $bucket,
    'Key'    => 'key1.gif',
    'Body'   => 'PATH_TO_FILE_1',
]);

$commands[] = $s3->getCommand('PutObject', [
    'Bucket' => $bucket,
    'Key'    => 'key2.gif',
    'Body'   => 'PATH_TO_FILE_2',
]);

$s3->execute($commands);

Thanks in advance!

How to&Answers:

Just solved this myself.

Instead of calling $s3->execute(), pass the client and the $commands array to a CommandPool like so:

use Aws\CommandPool;

// your code here

$results = CommandPool::batch($s3, $commands);

You’ll receive an array of results, sorted in the same order as the commands. One of the differeneces is that this will also include Exception objects if the command failed.