Last active
December 8, 2021 16:06
-
-
Save jessekanner/2cf7fdb26b21c3b2e268f0b21ff9c923 to your computer and use it in GitHub Desktop.
Read and Insert data into DynamoDB in Laravel controllers (and plain PHP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Reference: https://github.com/aws/aws-sdk-php-laravel | |
| ## Setup 1. Include AWS SDK into composer | |
| composer require aws/aws-sdk-php-laravel | |
| ## Setup 2. Publish AWS Class into Laravel | |
| php artisan vendor:publish --provider="Aws\Laravel\AwsServiceProvider" | |
| ## Setup 3. Include in providers (Laravel) | |
| 'providers' => array( | |
| // ... | |
| Aws\Laravel\AwsServiceProvider::class, | |
| ) | |
| //---- S-3 instructions here: https://github.com/aws/aws-sdk-php-laravel ----// | |
| //---- Read records from DynamoDB (Laravel example) ----// | |
| use AWS; | |
| $dynamo_db = AWS::createClient('dynamoDB'); | |
| $results_scan = $dynamo_db->getIterator('Scan', array( | |
| 'TableName' => 'your-dynamo-table-name', | |
| 'ScanFilter' => array( | |
| 'some_attribute_name' => array( | |
| 'AttributeValueList' => array( | |
| array('N' => '1') | |
| ), | |
| 'ComparisonOperator' => 'EQ' | |
| ) | |
| ) | |
| )); | |
| foreach ($results_scan as $item) { | |
| echo $item['attribute-name']['S']." ".$item['attribute-name']['S'] . "\n"; | |
| } | |
| //---- Insert record into DynamoDB (plain PHP example) ---// | |
| use Aws\DynamoDb\DynamoDbClient; | |
| $dynamoDbClient = DynamoDbClient::factory(array( | |
| 'profile' => 'dynamo-profile-name', | |
| 'version' => '2012-08-10', | |
| 'region' => 'us-east-2' | |
| )); | |
| // Store the confirmation keyed against the signup_uuid | |
| $timestamp = strval(round(microtime(true) * 1000)); | |
| $results_confirmation = $dynamoDbClient->putItem(array( | |
| 'TableName' => 'your-table-name', | |
| 'Item' => array( | |
| 'timestamp' => array('N' => $timestamp), | |
| 'some_attribute' => array('N' => $some_value), | |
| 'some_other_attribute' => array('S' => $some_other_value), | |
| ) | |
| )); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment