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!'),
);
}
}
use Path\To\AWS\Namespace\Class
in your class/fileuse
statements in your filesuse 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). Trycomposer 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