Error loading S3 Batch / Parallel using PHP SDK

I followed these codes until the PutObject in S3. I am using the latest version of the PHP SDK (3.x). But I get:

Argument 1 passed to Aws \ AwsClient :: execute () should implement the Aws \ CommandInterface interface given by the array

 $commands = array(); $commands[] = $s3->getCommand('PutObject', array( 'Bucket' => $bucket, 'Key' => 'images/1.jpg', 'Body' => base64_decode( 'xxx' ), 'ACL' => 'public-read', 'ContentType' => 'image/jpeg' )); $commands[] = $s3->getCommand('PutObject', array( 'Bucket' => $bucket, 'Key' => 'images/2.jpg', 'Body' => base64_decode( 'xxx' ), 'ACL' => 'public-read', 'ContentType' => 'image/jpeg' )); // Execute the commands in parallel $s3->execute($commands); 
+6
source share
1 answer

If you are using a modern version of the SDK, try creating the command this way. Taken straight from the docs ; he should work. This is called the chaining method.

 $commands = array(); $commands[] = $s3->getCommand('PutObject') ->set('Bucket', $bucket) ->set('Key', 'images/1.jpg') ->set('Body', base64_decode('xxx')) ->set('ACL', 'public-read') ->set('ContentType', 'image/jpeg'); $commands[] = $s3->getCommand('PutObject') ->set('Bucket', $bucket) ->set('Key', 'images/2.jpg') ->set('Body', base64_decode('xxx')) ->set('ACL', 'public-read') ->set('ContentType', 'image/jpeg'); // Execute the commands in parallel $s3->execute($commands); // Loop over the commands, which have now all been executed foreach ($commands as $command) { $result = $command->getResult(); // Use the result. } 

Make sure you are using the latest SDK.

Edit

It looks like the SDK API has changed significantly in version 3.x. The above example should work correctly in version 2.x of the AWS SDK. For 3.x you need to use CommandPool() and promise() :

 $commands = array(); $commands[] = $s3->getCommand('PutObject', array( 'Bucket' => $bucket, 'Key' => 'images/1.jpg', 'Body' => base64_decode ( 'xxx' ), 'ACL' => 'public-read', 'ContentType' => 'image/jpeg' )); $commands[] = $s3->getCommand('PutObject', array( 'Bucket' => $bucket, 'Key' => 'images/2.jpg', 'Body' => base64_decode ( 'xxx' ), 'ACL' => 'public-read', 'ContentType' => 'image/jpeg' )); $pool = new CommandPool($s3, $commands); // Initiate the pool transfers $promise = $pool->promise(); // Force the pool to complete synchronously try { $result = $promise->wait(); } catch (AwsException $e) { // handle the error. } 

Then $result should be an array of command results.

+4
source

All Articles