Third party library added via composer.json but how can I load it in a module now? 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'     =&gt; 'latest',
            'region'      =&gt; 'eu-west-2a',
            'credentials' =&gt; [
                'key'    =&gt; 'AbAbAbAbDcDcDc',
                'secret' =&gt; 'QwertyQweRty'
            ]
        ]);

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

}
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;