I have attached the policy to EC2 IAM role to access AWS services. After that i have have used below code in Yii 1 controller file:
ExampleController.php
class ExampleController extends Controller
{
public function init()
{
require_once dirname(dirname(__FILE__)) . '/extensions/awsv3/vendor/autoload.php';
$config = array(
'version' => 'latest',
'region' => 'us-west-2',
);
$s3_instance = new \Aws\Ssm\SsmClient($config);
$result = $s3_instance->getParameters([
'Names' => array('host_name'),
'WithDecryption' => true
]);
//converting S3 private data to array to read
$keys = $result->toArray();
var_dump($keys);
exit("Exit");
}
}
Output
PHP warning
include(TestRole.php): failed to open stream: No such file or directory
Note: TestRole is IAM Role Name.
I have used same code in Single PHP file (Not ties with Yii1)
test.php
require_once 'protected/extensions/awsv3/vendor/autoload.php';
$config = array(
'version' => 'latest',
'region' => 'us-west-2',
);
$s3_instance = new \Aws\Ssm\SsmClient($config);
$result = $s3_instance->getParameters([
'Names' => array('host_name'),
'WithDecryption' => true
]);
//converting S3 private data to array to read
$keys = $result->toArray();
var_dump($keys);
exit("Exit");
array(3) { ["Parameters"]=> array(1) { [0]=> array(3) { ["Name"]=> string(12) "host_name" ["Type"]=> string(6) "String" ["Value"]=> string(9) "localhost" } } ["InvalidParameters"]=> array(0) { } ["@metadata"]=> array(4) { ["statusCode"]=> int(200) ["effectiveUri"]=> string(35) "https://ssm.us-west-2.amazonaws.com" ["headers"]=> array(4) { ["x-amzn-requestid"]=> string(36) "d3fb85bc-da4e-494b-be4f-b31fe3814100" ["content-type"]=> string(26) "application/x-amz-json-1.1" ["content-length"]=> string(3) "182" ["date"]=> string(29) "Tue, 19 Jun 2018 12:28:50 GMT" } ["transferStats"]=> array(1) { ["http"]=> array(1) { [0]=> array(0) { } } } } } Exit
its working with single php file.
So question is how to fix it in Yii 1 and why its considering IAM Role Name as Class file?
I was able to fix this out, Thanks to @javierfdezg.
So basically, Yii's auto loader and AWS's auto loader was got conflicted and may be due to Yii's assumption that class names must match file names.
So first i have unregistered the Yii's auto load then after the api call is finished registered it again.
class ExampleController extends Controller
{
public function init()
{
/* Unregister YiiBase */
spl_autoload_unregister(array('YiiBase', 'autoload'));
require_once dirname(dirname(__FILE__)) . '/extensions/awsv3/vendor/autoload.php';
$config = array(
'version' => 'latest',
'region' => 'us-west-2',
);
$s3_instance = new \Aws\Ssm\SsmClient($config);
$result = $s3_instance->getParameters([
'Names' => array('host_name'),
'WithDecryption' => true
]);
/* Register YiiBase */
spl_autoload_register(array('YiiBase', 'autoload'));
$keys = $result->toArray();
var_dump($keys);
exit("Exit");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With