2

I am new to AWS. I have installed an EC2 server which processes PhP code. I am able to administrate the DB through the Amazon website. I'm trying to access my DynamoDB table with the following code:

use Aws\DynamoDb\DynamoDbClient;      
    try {             
        $client = DynamoDbClient::factory(array(
            'profile' => 'default', // access ID + secret are in the .aws/credentials file
            'region' => Region::EU_WEST_1 // also tried with "eu-west-1"
        ));              
        echo "after client instanciation"; // this is not displayed

        $response = $client->getItem([
            'TableName' => 'Child',
            'Key' => [
                'ChildID' => 'Nicolas'
                ]
        ]);
        print_r ($response['Item']);
    } catch (Exception $e) {
        echo '<p>Exception received : ',  $e->getMessage(), "\n</p>";
    }

I'm not getting any exception. The child I'm trying to get isn't displayed (I did create it). Also tried with the putItem method but it didn't add anything to the DB.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

2 Answers2

0

I think you are missing the data type in the key:

$response = $dynamodb->getItem([
'TableName' => 'Child',
'Key' => [
    'ChildID' => [ 'S' => 'Nicolas' ]
]

]);

Now you should get the output, you can refer this Link.

Harshal Bulsara
  • 7,898
  • 4
  • 43
  • 61
  • Hey, thanks for your answer. I tried it and it still doesn't work. I think the problem comes from the first call because the line "echo "after client instanciation";" isn't displayed. Any other idea? – Alexandre Raffin Oct 22 '15 at 07:43
  • can you add this and try again Aws\DynamoDb\Exception\DynamoDbException; – Harshal Bulsara Oct 22 '15 at 07:52
  • I added use Aws\DynamoDb\Exception\DynamoDbException; and a catch(DynamoDBException e){ echo '

    Exception received : ', $e->getMessage(), "\n

    "; } Still no exception.
    – Alexandre Raffin Oct 22 '15 at 08:52
  • I just tried providing the key and secret when instantiating the client. It seems weird to me that I don't get any error even if I put a wrong key/secret. – Alexandre Raffin Oct 22 '15 at 12:46
  • Did you check the php logs? can you modify factory with service builder, you can refer it from here http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-dynamodb.html – Harshal Bulsara Oct 23 '15 at 01:21
  • I ran cat php.ini | grep error_log. There were 2 results: error_log = php_errors.log and error_log = syslog. I went to /var/log and created php_errors.log because it didn't exist. I set the permissions to r+w. Then, I tried again to display the webpage and went to check php_errors.log but it was empty. I also tried with the other syntax, the service builder, it didn't work. I checked the content of aws-config.php and it seemed fine. – Alexandre Raffin Oct 23 '15 at 11:38
  • I hope the error reporting is on, here is the link if its not http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Harshal Bulsara Oct 24 '15 at 02:04
0

Try using the following code, it allows you to pass public and secret keys through parameters.

$client = new DynamoDbClient([
'version' => 'latest',
'region' => 'ap-northeast-1',
'credentials' => [
'key' => 'A5ITUTLAK7W47NNNNQ',
'secret' => 'DrsEjmEMs4PUPIY5/12a/cpUB7JVVcKLahFz826p'
]
]);

try {   
$result = $client->getItem(array(
'ConsistentRead' => true,
'TableName' => 'fruits',
'Key' => array(
'id' => array('S' => '1')
)
));

Replace the keys with yours.

Reference: https://solutionarchitects.guru/viewtopic.php?f=30&t=27