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

问题描述

我想用php写服务-

1) DynamoDB 将有表 t,其中包含两列 key 和 val

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

2) 我会检查表 t 中是否存在某个键.

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

3) 如果存在读取数据.. 如果不存在则在表 t 中插入新的键值

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

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

关注哪一个?

也有人可以给我简单的例子和​​准确的语法.

Also can someone give me quick example and exact syntax.

提前致谢.

推荐答案

完整的演练位于 HERE.它为您提供凭据设置过程的分步概述,并附带一个易于使用的附加到 PHP适用于 AWS 的开发工具包

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

  • 1. 转到 AWS 并获取您的 PUBLIC_KEYPRIVATE_KEY

  • 1. Go to AWS and get your PUBLIC_KEY and PRIVATE_KEY

2.打开终端

3. 如果您尚未创建凭据,请在终端的新页面中输入:

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

nano ~/.aws/credentials

  • nano 功能页面将打开.您将在顶部看到 GNU nano 2.0.6....
    • The nano function page will open up. You will see GNU nano 2.0.6... at the top.
    • 4.nano 页面中,输入:

      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. 输入后,按 CONTROL + X(是...控制,而不是命令).

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

      • 1.AWS_SDK_PHP 放入空文件夹
      • 2. 在使用 SDK(index.php 或其他)的文件顶部,输入:

      • 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 AwsDynamoDbDynamoDbClient;
      
      $client = new DynamoDbClient([
          'profile' => 'default',
          'region'  => 'us-east-1',
          'version' => 'latest'
      ]);
      


      • 数据类型
        • S = 字符串
        • N = 数字
        • B = 二进制
        • Data Types
          • S = String
          • N = Number
          • B = Binary


          • 基本方法

          • 描述表格

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

        • 放置物品

          $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;
          

        • 获取物品

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

        • 删除项目

          $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;
          

        • 查询项

          $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;
          

        • 希望这会有所帮助.

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

07-22 18:19