Search code examples
phpamazon-web-servicesaws-php-sdk

Error on instantiating class extended from AwsClient subclass


I'm extending my custom Sqs class like this:

class Sqs extends SqsClient
{
    public function __construct()
    {
        parent::__construct(array(
            'credentials' => array(
                'key' => $_ENV['AWS_ACCESS_KEY_ID'],
                'secret' => $_ENV['AWS_SECRET_ACCESS_KEY'],
            ),
            'region' => 'us-west-1',
            'version' => 'latest'
        ));
    }
}

And then I instantiate and use it like this:

$sqs = new Sqs();
$sqs->sendMessage([
    'QueueUrl' => $_ENV['AWS_SQS_URL_PREFIX'] . '/' . $_ENV['AWS_SQS_READER_USER_CREATE'],
    'MessageBody' => $user_json,
    'MessageGroupId' => 1,
    'MessageDeduplicationId' => uniqid(),
]);

But I'm getting a weird error:

The service \"\" is not provided by the AWS SDK for PHP.

Solution

  • The AWS APIs use reflection to figure out what AWS service they're connecting to based on the class name. If that's the case you might try calling your class SqsClient, eg

    use Aws\Sqs\SqsClient as BaseSqsClient;
    
    class SqsClient extends BaseSqsClient 
    {
         //...
    }