如何在 Laravel 中使用 dynamo db?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36742271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:38:33  来源:igfitidea点击:

how to use dynamo db with laravel?

phplaravelamazon-web-serviceslaravel-5amazon-dynamodb

提问by Naveen Singh

Hello I am new to dynamo db and laravel. After searching on the internet I found so many ways to setup the dynamo db for laravel and I tried each of them. but after setup the for using the dynamo db in laravel I cant found any tutorial or any sample codes so that I can use it further. some sites links which I used for dynamo db setup:

您好,我是 dynamo db 和 laravel 的新手。在互联网上搜索后,我找到了很多方法来为 laravel 设置 dynamo db,我尝试了其中的每一种。但是在设置了在 laravel 中使用 dynamo db 之后,我找不到任何教程或任何示例代码,以便我可以进一步使用它。我用于 dynamo db 设置的一些网站链接:

https://github.com/aws/aws-sdk-php-laravel

https://github.com/aws/aws-sdk-php-laravel

https://github.com/baopham/laravel-dynamodb

https://github.com/baopham/laravel-dynamodb

https://github.com/aws/aws-sdk-php

https://github.com/aws/aws-sdk-php

http://www.techigniter.in/tutorials/dynamodb-session-driver-for-laravel-5/

http://www.techigniter.in/tutorials/dynamodb-session-driver-for-laravel-5/

there are more sites also, all using their own different methods but did not helped me enough.

还有更多的网站,都使用自己不同的方法,但对我的帮助还不够。

回答by Krantikumar

It's very simple actually. You can use this SDK for Laravel: https://github.com/aws/aws-sdk-php-laravel

其实很简单。您可以将此 SDK 用于 Laravel:https: //github.com/aws/aws-sdk-php-laravel

This link can help you understand how to use it: https://stackoverflow.com/a/32594321/9845807

这个链接可以帮助你了解如何使用它:https: //stackoverflow.com/a/32594321/9845807

use AWS;
use Config;
public function dynamoDBConnect()
    {
        //Fetching credentials for DynamDB
        $config =  Config::get('aws');

        //Inserting value with tableName: "Logs" and columnName: "userId"
        $client = AWS::createClient('DynamoDb');
        $iterator = $client->getIterator('Query', array(
            'TableName'     => 'Logs',
            'KeyConditions' => array(
                'userId' => array(
                    'AttributeValueList' => array(
                        array('N' => '1202')
                    ),
                    'ComparisonOperator' => 'EQ'
                )
            )
        ));

        //Fetching the records.
        $result = $client->getItem(array(
            'ConsistentRead' => true,
            'TableName' => 'Logs',
            'Key'       => array(
                'userId'   => array('N' => '1201'),
                'Name'    => array('S' => 'Name string')
            )
        ));
    }

回答by Thushara Buddhika

Full Documentation: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.PHP.html

完整文档:https: //docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.PHP.html

Following method will connect to DynamoDB and retrieve data. Change ACCESS_KEY, SECRET_KEY, REGION, TABLE_NAME as yours.

以下方法将连接到 DynamoDB 并检索数据。将 ACCESS_KEY、SECRET_KEY、REGION、TABLE_NAME 更改为您的。

My DynamoDB Data is like, {'2019-11-05':'12:30-13:00', '2019-11-05':'13:30-14:00'}

我的 DynamoDB 数据就像,{'2019-11-05':'12:30-13:00', '2019-11-05':'13:30-14:00'}

public function getDataFromDynamoDb() {
$credentials = new Aws\Credentials\Credentials(ACCESS_KEY, SECRET_KEY);

$client = new Aws\Sdk([
    'version' => 'latest',
    'region' => REGION,
    'credentials' => $credentials
]);

$dynamodb = $client->createDynamoDb();
$marshaler = new Marshaler();

$tableName = 'TABLE_NAME';

$eav = $marshaler->marshalJson('{":Date": "2019-11-05"}');

$params = [
    'TableName' => $tableName,
    'KeyConditionExpression' => '#Date = :Date',
    'ExpressionAttributeNames'=> [ '#Date' => 'Date' ],
    'ExpressionAttributeValues'=> $eav
];

try {

    $result = $dynamodb->query($params);

    foreach ($result['Items'] as $marketData) {
        echo $marshaler->unmarshalValue($marketData['Date']) . ': ' .
            $marshaler->unmarshalValue($marketData['Value']) . "\n";
    }

} catch (DynamoDbException $e) {
    echo "Unable to query:\n";
    echo $e->getMessage() . "\n";
}}