Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument 2 passed to Aws\AwsClient::getCommand() must be of the type array, string given

I followed installation from this page.

Which is version 3 of the SDK. Basic usage here.

<?php
require 'vendor/autoload.php';

use Aws\CloudFront\CloudFrontClient;
$cdn = new CloudFrontClient(
['version' => 'latest',
 'region'  => 'us-east-1']
);

// Create a new invalidation.
$response = $cdn->create_invalidation('EPYAAAAAAPAA', 'aws-clear-cache' . time(), "/*");

According to this article.

Catchable fatal error: Argument 2 passed to Aws\AwsClient::getCommand() must be of the type array, string given, called in /Users/jason/www/aws/vendor/aws/aws-sdk-php/src/AwsClient.php on line 167 and defined in /Users/jason/www/aws/vendor/aws/aws-sdk-php/src/AwsClient.php on line 211

UPDATED & WORKING SOLUTION HERE:

$config = array(
                'region' => 'us-east-1',
                'version' => '2015-07-27',
                'credentials' => array(
                    'key'    => env('AMAZON_KEY_CLOUDFRONT'),  
                    'secret' => env('AMAZON_SECRET_CLOUDFRONT') 
                )
            );

        $cdn = new CloudFrontClient($config);

        $cache =    array('DistributionId' => env('CLOUDFRONT'),
                    'InvalidationBatch' => array(
                        'CallerReference' => 'none',
                        'Paths' => array(
                            'Quantity' => 1,
                            'Items' => array( 'test' => '/*')
                            )
                        )

            );

        // Create a new invalidation.
        $response = $cdn->createInvalidation($cache);
        var_dump($response);
like image 520
Jason Avatar asked Sep 22 '15 14:09

Jason


1 Answers

I took a look at what was going on here and apparently you're using an invalid method signature.

First of all, the AWS PHP SDK uses something called "service description models" to get an API interface for the version you specified. In this case that means they are using some meta-programming techniques to provide an interface to what looks like normal PHP function calls, but the functions are not hard-coded in the SDK. They don't really exist in the sense we generally think of, at least not as regular PHP functions. Some PHP magic is going on underneath.

When you call $cdn->create_invalidation() it runs on the Aws\CloudFront\CloudFrontClient instance which inherits from Aws\AwsClient. Neither of those classes (nor any of their ancestors) actually have a ::create_invalidation() method implementation. But AwsClient does implement the PHP magic method ::__call(). You can see the PHP docs for the full info on this magic method, but basically when you call any method that doesn't exist on an object, if its class implements ::__call(), then ::__call() will be invoked instead.

So now we're inside of ::__call() (you can see the code here) and this method ends up calling ::getCommand(). Inside of ::getCommand() you can see that the method signature requires an array to be passed in (the first argument, $name, is the only other method parameter and it's going to have the value "create_invalidation" because of the way ::__call() was implemented above). So that's where the the first problem crops up: you need to pass in an array, not individual strings or timestamps or anything else.

But there's one other thing; the actual method you want to call is not called create_invalidation, but createInvalidation. There are full API docs for the SDK here - just make sure you pick the right version. For the version I'm looking at, you can find the API docs and method signature for creating invalidations here.

You can find lots of information about the AWS PHP SDK including links to a User Guide, API Docs, and more, in the project's GitHub readme. Good luck and happy coding :)

like image 93
nc. Avatar answered Sep 20 '22 20:09

nc.