2

I have set up a module in my Drupal 8 project to handle some tasks I need doing using AWS.

I added the awssdk (for PHP) to my composer.json and ran the composer update command successfully. the AWS SDK is now sitting nicely in my /vendor directory under the folder aws.

Composer entry

'aws/aws-sdk-php': '^3.0'

I cannot seem to get this to load within my custom module. I have tried referencing the SDK as follows at the top of my one Controllers (After the use keyword but before the class keyword)

require_once (\Drupal::root() . '/vendor/autoload.php');

The result of trying this gives me a blank white page with this error in the apache log:

PHP message: PHP Fatal error: require_once(): Failed opening required '/home/dash/public_html/docroot/vendor/autoload.php' (include_path='.:/usr/share/php') in /home/dash/public_html/docroot/modules/custom/awsintegration/src/Controller/AwsiInstanceListController.php on line 9

Also tried using an absolute path as

require '/home/dash/public_html/vendor/autoload.php';

Result: Page loads no watchdog error but I get an apache php error as soon as I try reference one of the SDK classes:

PHP message: PHP Fatal error: Class 'Drupal\awsintegration\Controller\Aws\Sdk' not found in /home/dash/public_html/docroot/modules/custom/awsintegration/src/Controller/AwsiInstanceListController.php on line 27

UPDATE

After Clive's suggestion in comments I removed the require calls. I then added some simple code that used AWS to try create a credentials provider, I simply get errors in the apache log about missing Class as per:

PHP Fatal error: Class 'Drupal\awsintegration\Controller\Aws\S3\S3Client' not found in /home/dash/public_html/docroot/modules/custom/awsintegration/src/Controller/AwsiInstanceListController.php on line 21

My code is as follows:

namespace Drupal\awsintegration\Controller;

use Drupal\awsintegration\AwsInstances;
use Drupal\Core\Controller\ControllerBase;

use Aws\Credentials\CredentialProvider;
use Aws\S3\S3Client;

class AwsiInstanceListController extends ControllerBase {

    public function content() {

        $s3 = new Aws\S3\S3Client([
            'version'     => 'latest',
            'region'      => 'eu-west-2a',
            'credentials' => [
                'key'    => 'AbAbAbAbDcDcDc',
                'secret' => 'QwertyQweRty'
            ]
        ]);

        return array(
            '#type' => 'markup',
            '#markup' => $this->t('Hello, World!'),
        );
    }

}
  • 2
    Composer handles the autoloading, you should only need to use Path\To\AWS\Namespace\Class in your class/file – Clive Nov 24 '17 at 11:14
  • Thanks for the reply Clive, is a libraries entry needed in one of my module YAML files ? e.g. *.libraries.yml or *.info.yml – John Cogan Nov 24 '17 at 11:31
  • 1
    Nope, autoloading is a level or two above that. Composer does the thinking for you, you just need to have the correct use statements in your files – Clive Nov 24 '17 at 11:37
  • Simply getting class no found error. I am sure the aws files exist, just unsure if composer is loading them or not. Also in PHPStorm its not autocompleting the path when typing in the use statement, but that might be due to PHPStorm and not the framework – John Cogan Nov 24 '17 at 11:55
  • 1
    Probably not, PHPStorm is pretty epic at resolving class paths (unless it's in the middle of an index). You can see from their docs that you just need use Aws\S3\S3Client; at the top of the file (the autoload require is redundant since Drupal/Symfony does that anyway otherwise nothing would work at all). Try composer dump-autoload as a last resort, but if you've required the AWS library, and it's been installed into /vendor, you've technically done everything you need to – Clive Nov 24 '17 at 12:02
1

You don't need to use require_once for autoload. It handles by the composer automatically. You should create a use statement in your php file.

As an example if you want to use a class s3 module of aws;

use Aws\S3\SomeClassName;
  • Curious, Clive in the comments above addresses this as you stated, but for some reason the sdk is not been picked up. Appreciated the info though. – John Cogan Nov 24 '17 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.