需要一个使用php的DynamoDB的完整示例 [英] Need a complete example for DynamoDB with php

查看:1532
本文介绍了需要一个使用php的DynamoDB的完整示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php中写入服务 -



1)DynamoDB将使用两个列键和val

的表t

2)我将检查表t中是否存在某个键。



3)如果存在读取数据..如果不存在insert new表t中的键值



我正在检查一些链接
http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonDynamoDB/put_item
http://docs.aws.amazon.com /aws-sdk-php/guide/latest/quick-start.html



要跟哪一个?



也可以有人给我快速的例子和确切的语法。



提前感谢。

解决方案

完整演练位于 HERE 。它为您提供了凭证设置过程的一步一步概述,并附带了一个易于使用的 PHP适用于AWS的SDK



AWS设置

- AWS不会为您设置的凭据设置一步,所以我会的。


  • 1。前往AWS并取得 PUBLIC_KEY PRIVATE_KEY




  • 2。打开终端


  • 但是在Terminal的新页面中输入:

      nano〜/ .aws / credentials 




    • 将打开 nano 功能页面。您将在顶部看到 GNU nano 2.0.6 ...


  • 4。 nano 页面中输入:

      [default] 
    aws_access_key_id = public_key_ABCDEFGHIJKLMNOPQRSTUVWXYZ
    aws_secret_access_key = private_key_s0m3_CR42Y_l3tt3rS_i5y0ur53cr3tK3y

  • $ b $
    (是...控制,而不是命令)

    Y ,然后按 ENTER strong> 7。访问您的Elastic Beanstalk
    li> 9。完成创建应用后,您会看到带有绿色勾号的概述屏幕。 在软件配置 - > 属性名称 - > 中输入 AWS_ACCESS_KEY_ID 键入 AWS_SECRET_KEY ,输入: [your_secret_key]
  • 13。 。将所有文件放入一个文件夹。将文件夹命名为 [whatever] ,然后压缩 [无论] 中的文件。不要压缩整个文件夹。只压缩文件夹中的文件。如果其中一个档案包含 index.php index.html ,您的专案就会显示在EBS预设网址上。

  • 14。。您的项目应名为 Archive.zip (Mac)。去EBS,上传zip,有你去!所有完成AWS安装程序!



AWS设置


  • 1。 AWS_SDK_PHP 添加到空文件夹

  • 2。在文件顶部使用SDK( index.php 或者其他),放在:

      require'aws / aws-autoloader.php'; 
    date_default_timezone_set('America / New_York');

    使用Aws \DynamoDb\DynamoDbClient;

    $ client = new DynamoDbClient([
    'profile'=>'default',
    'region'=>'us-east-1',
    'version'=>'latest'
    ]);








  • 数据类型


    • strong> =字符串

    • N =数字









  • 基本方法




    •   $ result = $ client-> describeTable(array(
      'TableName'=& '[Table_Name]'
      ));

      echo $ result;


    • 输入项

        $ response = $ client-> putItem(array(
      'TableName'=>'[Table_Name]',
      ' => array(
      '[Hash_Name]'=> array('S'=>'[Hash_Value]'),
      '[Range_Name]'= >'[Range_Value]')

      ));

      //响应响应只好检查它是否成功。状态:200 =成功
      echo $ response;


    • 获取项 b $ b

        $ response = $ client-> getItem(array(
      'TableName'=>'[Table_Name]',
      ' => array(
      '[Hash_Name]'=> array('S'=>'[Hash_Value]'),
      '[Range_Name]'= >'[Range_Value]')

      ));

      echo $ response;


    • 删除项目

        $ response = $ client-> deleteItem(array(
      'TableName'=>'[Table_Name]',
      ' => array(
      '[Hash_Name]'=> array('S'=>'[Hash_Value]'),
      '[Range_Name]'= >'[Range_Value]')

      ));
      //回显响应只好检查它是否成功。状态:200 =成功
      echo $ response;


    • 查询项

        $ response = $ client-> query(array(
      'TableName'=>'[Table_Name]',
      'KeyConditionExpression' =>'[Hash_Name] =:v_hash和[Range_Name] =:v_range',
      'ExpressionAttributeValues'=> array(
      ':v_hash'=& '[Hash_Value]'),
      ':v_range'=> array('S'=>'[Range_Value]')



      echo $ response;





希望这有助。


I want to write service in php where -

1) DynamoDB will have table t with two columns key and val

2) I will check if some key exists in table t or not.

3) If exist read data .. if don't exist insert new key-value in table t

I was checking some links http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonDynamoDB/put_item http://docs.aws.amazon.com/aws-sdk-php/guide/latest/quick-start.html

Which one to follow ?

Also can someone give me quick example and exact syntax.

Thanks in advance.

解决方案

The full walkthrough is located HERE. It gives you a step by step outline of the setup process for credentials and comes with a easy to use add on to the PHP SDK for AWS

AWS Setup - AWS doesn't lay out the Credentials setup for you set by step, so I will.
  • 1. Go to AWS and get your PUBLIC_KEY and PRIVATE_KEY

    • AWS does have tutorials for this HERE and HERE
  • 2. Open Terminal

  • 3. If you haven't created your credentials yet, in Terminal's fresh page, type in:

    nano ~/.aws/credentials
    

    • The nano function page will open up. You will see GNU nano 2.0.6... at the top.
  • 4. Inside the nano page, type in:

    [default]
    aws_access_key_id = public_key_ABCDEFGHIJKLMNOPQRSTUVWXYZ
    aws_secret_access_key = private_key_s0m3_CR42Y_l3tt3rS_i5y0ur53cr3tK3y
    

  • 5. Once you've typed it out, hit CONTROL + X (Yes...Control, not Command).

  • 6. Hit Y then ENTER
  • 7. Get the [AWS_SDK_PHP][4]
  • 8. Go to Your Elastic Beanstalk
  • 9. When you are done creating your app and you see the Overview screen with the green check. Look to the side and hit Configuration.
  • 10. Under Software Configuration -> Document root: type in: /
  • 11. Under Property Name -> AWS_ACCESS_KEY_ID type in: [your_access_key]
  • 12. Under AWS_ACCESS_KEY_ID is the AWS_SECRET_KEY, type in: [your_secret_key]
  • 13. When your PHP Project is ready. Put all your files into one folder. Name the folder [whatever], then compress the files inside [whatever]. Do not compress the whole folder. Only compress the files in the folder. If one of these files includes your index.php or index.html, your project will show up on EBS default URL.
  • 14. Your project should be called Archive.zip (Mac). Go to EBS, upload the zip and there you go! All finished with AWS Setup!
AWS Setup
  • 1. Put the AWS_SDK_PHP in an empty folder
  • 2. At the top of the file using the SDK (index.php or whatever), put in:

    require 'aws/aws-autoloader.php';
    date_default_timezone_set('America/New_York');
    
    use Aws\DynamoDb\DynamoDbClient;
    
    $client = new DynamoDbClient([
        'profile' => 'default',
        'region'  => 'us-east-1',
        'version' => 'latest'
    ]);
    


  • Data Types
    • S = String
    • N = Number
    • B = Binary


  • The Basic Methods

    • Describe Table

      $result = $client->describeTable(array(
          'TableName' => '[Table_Name]'
      ));
      
      echo $result;
      

    • Put Item

      $response = $client->putItem(array(
          'TableName' => '[Table_Name]', 
          'Item' => array(
              '[Hash_Name]'   => array('S' => '[Hash_Value]'),
              '[Range_Name]'  => array('S' => '[Range_Value]')
          )
      ));
      
      //Echoing the response is only good to check if it was successful. Status: 200 = Success
      echo $response;
      

    • Get Item

      $response = $client->getItem(array(
          'TableName' => '[Table_Name]',
          'Key' => array(
              '[Hash_Name]'  => array('S' => '[Hash_Value]'),
              '[Range_Name]' => array('S' => '[Range_Value]')
          )
      ));
      
      echo $response;
      

    • Delete Item

      $response = $client->deleteItem(array(
          'TableName' => '[Table_Name]',
          'Key' => array(
              '[Hash_Name]'  => array('S' => '[Hash_Value]'),
              '[Range_Name]' => array('S' => '[Range_Value]')
          )
      ));
      //Echoing the response is only good to check if it was successful. Status: 200 = Success
      echo $response;
      

    • Query Item

      $response = $client->query(array(
          'TableName' => '[Table_Name]',
          'KeyConditionExpression' => '[Hash_Name] = :v_hash and [Range_Name] = :v_range',
          'ExpressionAttributeValues' =>  array (
              ':v_hash'  => array('S' => '[Hash_Value]'),
              ':v_range' => array('S' => '[Range_Value]')
          )
      ));
      
      echo $response;
      

Hope this helps.

这篇关于需要一个使用php的DynamoDB的完整示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆