简体   繁体   English

[AWS] [DynamoDB]通过PhP SDK连接到数据库

[英][AWS][DynamoDB] Connection to the DB through the PhP SDK

I am new to AWS. 我是AWS的新手。 I have installed an EC2 server which processes PhP code. 我已经安装了处理PhP代码的EC2服务器。 I am able to administrate the DB through the Amazon website. 我可以通过亚马逊网站管理数据库。 I'm trying to access my DynamoDB table with the following code: 我正在尝试使用以下代码访问DynamoDB表:

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. 还尝试了putItem方法,但未向数据库添加任何内容。

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 . 现在您应该获得输出,可以参考此Link

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 参考: https : //solutionarchitects.guru/viewtopic.php?f=30&t=27

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM